为来自 terra 的 c 函数添加前缀
Prefixing the c function from terra
在 R 中,我可以在函数前面加上它们所属的包的名称(例如,dplyr::select
)。尽管如此,我在使用 terra
包中的 c
执行此操作时遇到了问题。我可以用 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
的导出函数,但我可以很好地访问和使用它...这么久因为我不使用前缀。
问: 谁能解释为什么会这样,以及我如何从 terra
中明确引用 c
?
PS ?terra::c
给出了一个帮助页面,解释了 c
如何将 SpatRaster
对象组合成一个新的 SpatRaster
对象,这向我暗示了这个函数必须 在 terra
包中实现。
这是因为 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
我刚刚在开发版中修复了这个问题;和那个版本上面 returns
#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
在 R 中,我可以在函数前面加上它们所属的包的名称(例如,dplyr::select
)。尽管如此,我在使用 terra
包中的 c
执行此操作时遇到了问题。我可以用 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
的导出函数,但我可以很好地访问和使用它...这么久因为我不使用前缀。
问: 谁能解释为什么会这样,以及我如何从 terra
中明确引用 c
?
PS ?terra::c
给出了一个帮助页面,解释了 c
如何将 SpatRaster
对象组合成一个新的 SpatRaster
对象,这向我暗示了这个函数必须 在 terra
包中实现。
这是因为 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
我刚刚在开发版中修复了这个问题;和那个版本上面 returns
#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