在R中,我可以在函数前面加上它们所属的包的名称(例如,dplyr::select)。尽管如此,我在使用来自c包的terra时遇到了问题。我可以在base::c上做得很好(如果我想的话):
base::c(1, 2, 3)
# [1] 1 2 3 然而,在为terra运行类似的代码时,我遇到了问题。
# Dummy SpatRaster
foo <- terra::rast(matrix(1:9, ncol=3))
# Works fine
c(foo, foo)
# Not so much
terra::c(foo, foo)
# Error: 'c' is not an exported object from 'namespace:terra'我很困惑为什么c不是terra的导出函数,但是我可以很好地访问和使用它。只要我不用前缀。
Q:可以有人解释为什么会这样,以及我如何显式地引用terra的c
PS ?terra::c给出了一个帮助页面,解释c如何将SpatRaster对象组合成一个新的SpatRaster对象,这向我建议这个函数一定是在terra包中实现的。
发布于 2020-12-04 03:04:09
这是因为c是一个“原始”函数--它们遵循自己的规则。包不能/不需要导入和导出它们。
c
#function (...) .Primitive("c")但情况并非如此,例如,nrow
nrow
#function (x)
#dim(x)[1L]
#<bytecode: 0x000000001662d228>
#<environment: namespace:base>terra从它创建一个泛型函数,并导出它,以便您可以执行terra::nrow()。
您的问题让我更加关注这个问题,并且我注意到,在terra的当前版本中,如果您不使用library(terra)加载包,许多原始函数就无法工作。例如,你可以得到
foo <- terra::rast(matrix(1:9, ncol=3))
x <- c(foo, foo)
max(x)
#Error in x@ptr$summary(fun, na.rm, .terra_environment$options@ptr) :
# trying to get slot "ptr" from an object of a basic class ("NULL") with no slots 我只是在开发版本中修正了这个问题,然后使用该版本返回上述内容。
#class : SpatRaster
#dimensions : 3, 3, 1 (nrow, ncol, nlyr)
#resolution : 1, 1 (x, y)
#extent : 0, 3, 0, 3 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +no_defs
#source : memory
#names : max
#min values : 1
#max values : 9 https://stackoverflow.com/questions/65127796
复制相似问题