R:一般的方法不应该在一个包内部没有连接的情况下工作吗? [英] R: Shouldn't generic methods work internally within a package without it being attached?
问题描述
我正在编写一个使用空间 sp
库中的类和函数的包。 sp
导出方法 rbind
(我在调用 rbind
通用?)。例如,下面的代码创建两个 SpatialPoints 对象,然后使用 rbind.SpatialPoints
将它们连接在一起:
> crdsA< - 矩阵(c(1,2,3,4),ncol = 2)
> crdsB< - 矩阵(c(7,8),ncol = 2)
>
> sptsA< - sp :: SpatialPoints(crdsA)
> sptsB< - sp :: SpatialPoints(crdsB)
>
> sp :: rbind.SpatialPoints(sptsA,sptsB)
SpatialPoints:
coords.x1 coords.x2
[1,] 1 3
[2,] 2 4
[3,] 7 8
坐标参照系(CRS)参数:不是
然而,然后将 SpatialPoints 转换为 SpatialPointsDataFrame ( sp
库中的高级对象类),然后使用 rbind.SpatialPointsDataFrame
,出现错误:
> sptsdfA< - sp :: SpatialPointsDataFrame(sptsA,data.frame(IDs = c(1,2)))
> sptsdfB< - sp :: SpatialPointsDataFrame(sptsB,data.frame(IDs = 3))
>
> sp :: rbind.SpatialPointsDataFrame(sptsdfA,sptsdfB)
rbind2(.. 1,r)中的错误:
没有将此S4类强制为一个向量的方法
查看 rbind.SpatialPointsDataFrame
源代码显示它调用 rbind
SpatialPoints :
rbind.SpatialPointsDataFrame< - function(...){
(点),函数(x)作为(x,());
名称(点)< - NULL#bugfix Clement Calenge 100417
sp = do.call(rbind,lapply SpatialPoints)))
df = do.call(rbind,lapply(dots,function(x)x @ data))
SpatialPointsDataFrame(sp,df,coords.nrs = dots [[1]] @ coords.nrs)
}
所以这似乎是问题所在,但我确实不明白为什么。如果我附加 sp
库,那么这些问题都不会发生,但我认为既然内部调用了 rbind
在 rbind.SpatialPointsDataFrame
内,那么库的其余部分不会被附加。
在上下文中我创建的包,即使我包含 import(sp)
和 importFrom(sp,rbind.SpatialPoints)
NAMESPACE,上面的代码不起作用。
我认为很明显,我不了解加载,附加和导入包的问题。任何人都可以解释为什么 sp :: rbind.SpatialPointsDataFrame
在没有附加其他库的情况下无法工作,以及如何让它在我的包中工作?
非常感谢!
rbind code>不是一般的泛型:因为它的签名有
...
作为它的第一个(也是唯一的)参数,所以它不能在第一个参数上发送。在所有情况下为其编制方法都是有问题的。你看过 maptools :: spRbind
?
I am writing a package that uses classes and functions from the spatial sp
library. sp
exports methods for rbind
(am I correct in calling rbind
a generic?).
For instance, the following code creates two SpatialPoints objects and then uses rbind.SpatialPoints
to join them together:
> crdsA <- matrix(c(1,2,3,4), ncol = 2)
> crdsB <- matrix(c(7,8), ncol = 2)
>
> sptsA <- sp::SpatialPoints(crdsA)
> sptsB <- sp::SpatialPoints(crdsB)
>
> sp::rbind.SpatialPoints(sptsA, sptsB)
SpatialPoints:
coords.x1 coords.x2
[1,] 1 3
[2,] 2 4
[3,] 7 8
Coordinate Reference System (CRS) arguments: NA
However, if I then convert the SpatialPoints to SpatialPointsDataFrame (a higher level object class within the sp
library), and then use rbind.SpatialPointsDataFrame
, I get an error:
> sptsdfA <- sp::SpatialPointsDataFrame(sptsA, data.frame(IDs = c(1,2)))
> sptsdfB <- sp::SpatialPointsDataFrame(sptsB, data.frame(IDs = 3))
>
> sp::rbind.SpatialPointsDataFrame(sptsdfA, sptsdfB)
Error in rbind2(..1, r) :
no method for coercing this S4 class to a vector
A look at the rbind.SpatialPointsDataFrame
source code reveals that it calls rbind
for SpatialPoints:
rbind.SpatialPointsDataFrame <- function(...) {
dots = list(...)
names(dots) <- NULL # bugfix Clement Calenge 100417
sp = do.call(rbind, lapply(dots, function(x) as(x, "SpatialPoints")))
df = do.call(rbind, lapply(dots, function(x) x@data))
SpatialPointsDataFrame(sp, df, coords.nrs = dots[[1]]@coords.nrs)
}
So this seems to be the problem, but I do not understand why. If I attach the sp
library, then none of these problems occur, but I thought that since rbind
was being called internally within rbind.SpatialPointsDataFrame
, then the rest of the library did not to be attached.
Within the context of the package I am creating, even if I include import(sp)
and importFrom(sp,rbind.SpatialPoints)
in the NAMESPACE, the code above does not work.
I guess there is clearly something I am not understanding with regards to loading, attaching and importing packages. Could anyone explain why sp::rbind.SpatialPointsDataFrame
does not work without the rest of the library being attached, and how I can get it to work within my package?
Thanks a lot!
rbind
is not a usual generic: since its signature has ...
as its first (and only) argument, it cannot dispatch on the first argument. It has been problementic in all cases to program methods for it. Have you looked at maptools::spRbind
?
这篇关于R:一般的方法不应该在一个包内部没有连接的情况下工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!