gpt4 book ai didi

r - 使用 Roxygen 记录 setAs() 和 setOldClass()

转载 作者:行者123 更新时间:2023-12-04 15:14:56 24 4
gpt4 key购买 nike

我正在开发一个提供名为“Foo”的 S3 类的包。它还提供了一个“as”方法,用于将其强制转换为(其他人的)名为“Bar”的 S4 类。我的代码如下所示:

#' ...
setOldClass("Foo")

#' ...
setAs("Foo", "SpatialPointsDataFrame", function(from) {
# do stuff and return a SpatialPointsDataFrame
})

编辑我试过这个:
#' ...
#' @name as
#' @export
setAs("Foo", "SpatialPointsDataFrame", function(from) {
# do stuff and return a SpatialPointsDataFrame
})

但后来我从 R CMD CHECK 得到了这个:

checking whether the name space can be loaded with stated dependencies ... WARNING Error in namespaceExport(ns, exports) : undefined exports: as Calls: loadNamespace -> namespaceExport Execution halted

A namespace must be able to be loaded with just the base namespace loaded: otherwise if the namespace gets loaded by a saved object, the session will be unable to start.

Probably some imports need to be declared in the NAMESPACE file.



在单独的 .R 文件中,我有:
#' @importClassesFrom sp SpatialPointsDataFrame

我正在使用 hadley 的 devtools 包,所以我猜它是 roxygen2。这就是我所做的:
R> document("MyPackage")

最佳答案

roxygen2 解析器没有解析 setOldClass()setAs() .
我们需要获取合适的@name标签。

#' "Foo" class
#'
#' @name Foo-class
#' @aliases Foo
#' @family Foo
#'
#' @exportClass Foo
setOldClass("Foo")

#' As("Foo", "SpatialPointsDataFrame")
#'
#' @name as
#' @family Foo
#'
#' @importClassesFrom sp SpatialPointsDataFrame
setAs("Foo", "SpatialPointsDataFrame", function(from) {
# do stuff and return a SpatialPointsDataFrame
})

我不知道 setAs()功能详细,但 as()函数是从方法包中加载的。
所以,我认为我们不需要 export(as) NAMESPACE 中的条目。

关于r - 使用 Roxygen 记录 setAs() 和 setOldClass(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7758748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com