从另一个包导入S3方法 [英] Importing S3 method from another package
问题描述
我正在尝试从另一个包pls
导入S3方法predict
.我有一个使用这些预测值的函数.问题是,在编译软件包时:
Error : object 'predict' is not exported by 'namespace:pls'
我整理了此要点,作为强调我的问题和解决问题的最小示例包含以下R文件:
#' Test function
#'
#' @importFrom pls predict
#'
#' @export
myfunc <- function(x){
stopifnot(class(x) == "mvr")
predict(x)*2
}
将其概括为原始内容(如下)已经过时,并且存在错误或误导之处.
最接近的问题是, pls 程序包中没有名为predict
的函数;有一些未导出的S3方法用于predict
,但没有这样的predict
.因此,您无法导入此文件. predict
通用名称位于 stats 包中,您需要从那里导入,如下所述.
您的软件包必须在DESCRIPTION
中具有Depends: pls
,才能使R可以使用正确的predict
方法. pls 中没有您可以专门导入的内容.>
您还需要从 stats 名称空间导入predict
泛型,因此添加
#' @importFrom stats predict
因为这将在您的包命名空间中导入泛型.您还需要将Imports: stats
添加到您的DESCRIPTION
文件中,以表明您需要 stats 包;以前,我们不必声明R附带的基础软件包集(即R附带的非推荐软件包)的依赖性.
原始
这里的主要问题是 pls 没有定义函数/方法predict
.它为predict
泛型提供了几种方法,但没有提供泛型本身.
如果需要,您需要从 stats 包中导入泛型-我不确定您这样做是因为您没有创建需要或基于泛型的函数.至少您需要
#' @importFrom stats predict
尽管您可能需要/想要导入整个 stats 名称空间-取决于您程序包的功能超出了您当前正在使用的功能.
另一个问题是predict.mvr
是不是从 pls 名称空间
> require(pls)
Loading required package: pls
Attaching package: ‘pls’
The following object(s) are masked from ‘package:stats’:
loadings
> predict.mvr
Error: object 'predict.mvr' not found
> pls::predict.mvr
Error: 'predict.mvr' is not an exported object from 'namespace:pls'
> pls:::predict.mvr
function (object, newdata, ncomp = 1:object$ncomp, comps, type = c("response",
"scores"), na.action = na.pass, ...)
因此,您不能只导入它.因此,您的软件包需要在DESCRIPTION
中包含Depends: pls
,以便找到正确的predict
方法.
I'm trying to import a S3 method, predict
from another package pls
. I have a function which uses these predicted values. The problem is, when compiling the package:
Error : object 'predict' is not exported by 'namespace:pls'
I've put together this Gist as a minimal example which highlights my problem and contains the following R file:
#' Test function
#'
#' @importFrom pls predict
#'
#' @export
myfunc <- function(x){
stopifnot(class(x) == "mvr")
predict(x)*2
}
To summarise this as the original (below) is now out-dated and in places erroneous or misleading.
The proximal issue is that there is no function named predict
in the pls package; there are some unexported S3 methods for predict
but no such predict
. So you can't import this. The predict
generic lives in the stats package and you'll need to import from there as discussed below.
Your package needs to have Depends: pls
in the DESCRIPTION
in order for the correct predict
method to be available to R. There's nothing in pls that you can specifically import.
You also need to import the predict
generic from the stats namespace, so add
#' @importFrom stats predict
as that will import the generic in you packages namespace. You will also want to add Imports: stats
to your DESCRIPTION
file to indicate that you need the stats package; previously we didn't have to declare dependencies on the set of base packages shipped with R (i.e. the non-Recommended ones that ship with R).
Original
The main issue here is the pls doesn't define a function/method predict
. It provides several methods for the predict
generic, but not the generic itself.
You need to import the generic from the stats package, if you need it - I'm not sure you do as you aren't creating a function that needs or builds on the generic. At the minimum you'll need
#' @importFrom stats predict
though you may need/want to import the entire stats namespace - depends what your package does beyond the function your are currently working on.
The other issue is that predict.mvr
is not exported from the pls namespace
> require(pls)
Loading required package: pls
Attaching package: ‘pls’
The following object(s) are masked from ‘package:stats’:
loadings
> predict.mvr
Error: object 'predict.mvr' not found
> pls::predict.mvr
Error: 'predict.mvr' is not an exported object from 'namespace:pls'
> pls:::predict.mvr
function (object, newdata, ncomp = 1:object$ncomp, comps, type = c("response",
"scores"), na.action = na.pass, ...)
As such you can't just import it. Hence your package needs to have Depends: pls
in the DESCRIPTION
in order for the correct predict
method to be found.
这篇关于从另一个包导入S3方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!