[英]Importing S3 method from another package
I'm trying to import a S3 method, predict
from another package pls
. 我试图导入S3方法,
predict
从另一个包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: 我把这个Gist放在一起作为一个最小的例子,它突出了我的问题,并包含以下R文件:
#' 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; 最近的问题是pls包中没有名为
predict
函数; there are some unexported S3 methods for predict
but no such predict
. 有一些未经
predict
S3方法可用于predict
但没有这样的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. predict
通用版存在于stats包中,您需要从那里导入,如下所述。
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. 你的包需要有
Depends: pls
在DESCRIPTION
,以便正确的predict
方法提供给R.有没有在请 ,你可以专门进口。
You also need to import the predict
generic from the stats namespace, so add 您还需要从stats命名空间导入
predict
通用,因此请添加
#' @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; 您还需要将
Imports: stats
添加到DESCRIPTION
文件中,以指示您需要stats包; previously we didn't have to declare dependencies on the set of base packages shipped with R (ie the non-Recommended ones that ship with R). 以前我们不必声明R附带的基本软件包集合的依赖关系(即R附带的非推荐的基础软件包)。
The main issue here is the pls doesn't define a function/method predict
. 这里的主要问题是pls没有定义函数/方法
predict
。 It provides several methods for the predict
generic, but not the generic itself. 它为
predict
泛型提供了几种方法,但不提供通用本身。
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. 如果需要,您需要从stats包中导入泛型 - 我不确定您是否这样做,因为您没有创建需要或基于泛型的函数。 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 另一个问题是
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, ...)
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. 因此,您的包需要具有
Depends: pls
参阅DESCRIPTION
中的DESCRIPTION
,以便找到正确的predict
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.