简体   繁体   English

从另一个包导入S3方法

[英]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: plsDESCRIPTION ,以便正确的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附带的非推荐的基础软件包)。


Original 原版的

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.

相关问题 从另一个包导入S3泛型 - Importing S3 generics from another package 如何在不加载包的情况下从另一个包扩展 S3 方法 - How to extend S3 method from another package without loading the package 导入包的 S3 方法而不导入其函数 - Importing a package's S3 methods without importing its functions 从未定义泛型函数的包导入S3方法 - Import an S3 method from a package that does not define the generic function 从卸载的R包中使用S3方法 - Using a S3 method from an unloaded R package 如何在其命名空间中使用导出而不是S3method的其他包中的S3方法,而不使用Depends或library() - How to use S3 methods from another package which uses export rather than S3method in its namespace without using Depends or library() S3通用方法没有出现在包装手册中 - S3 generic method not appearing in package manual 已注册的 S3 方法被“GGally”覆盖:来自 +.gg 的方法 - RStudio 在不运行代码的情况下加载 package? - Registered S3 method overwritten by 'GGally': method from +.gg - RStudio loading package without running code? 从Matrix包导入S4功能 - Importing S4 functions from the Matrix package 如何使用 Roxygen 正确记录来自不同 package 的通用 S3 方法? - How to properly document a S3 method of a generic from a different package, using Roxygen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM