简体   繁体   English

我是否必须导出通过::在R中有条件导入的函数?

[英]Do I have to export functions conditionally imported via :: in R?

I am extracting information from objects whose classes are defined in various R packages. 我正在从在各种R包中定义了类的对象中提取信息。 For example, I extract coefficients from various statistical models (for which coef methods are not always implemented). 例如,我从各种统计模型(不总是采用coef方法)中提取系数。 I usually don't have to import those packages because I defined a generic function for which methods can be added by users. 我通常不必导入那些软件包,因为我定义了一个通用函数,用户可以为其添加方法。 There is one method for each kind of statistical model, and it would be stupid to import all those model definitions if the user is only interested in one specific model type. 每种统计模型都有一种方法,如果用户仅对一种特定的模型类型感兴趣,那么导入所有这些模型定义将是愚蠢的。

In some cases, however, I need to use a function which is defined in a package. 但是,在某些情况下,我需要使用在包中定义的函数。 For instance, the confint.merMod method in the lme4 package. 例如,lme4包中的confint.merMod方法。 Up to now, I used package::function to call these functions and wrapped this command in an exists(function) if-condition to make sure that the package really offers this function (because the function may be available only in some versions of the package). 到目前为止,我使用package::function来调用这些函数,并将此命令包装在一个exists(function) if-condition中,以确保该软件包确实提供了此功能(因为该功能可能仅在某些版本的包)。

However, I just discovered on http://developer.r-project.org/blosxom.cgi/R-devel/NEWS (see Sep 5, 2013) that in R version 3.0.2, "'R CMD check' does more thorough checking of declared packages and namespaces. It reports [...] objects imported by '::' which are not exported." 但是,我只是在http://developer.r-project.org/blosxom.cgi/R-devel/NEWS (请参阅2013年9月5日)上发现,在R版本3.0.2中,“'R CMD check'可以完成更多工作彻底检查已声明的包和名称空间。它报告由“ ::”导入的对象,这些对象不会导出。

Does this mean that I really have to add export("function") to the NAMESPACE file? 这是否意味着我真的必须将export("function")到NAMESPACE文件中? Wouldn't the CMD check complain because the function is only imported conditionally? CMD不会因为功能只是有条件地导入而抱怨吗?

To clarify/summarize for future visitors... 为了澄清/总结未来的访客...

The R-devel News quoted in the question related to the now 3-0-branch NEWS file where the specific entry lists the particular cases which R CMD check will generate a report for issues found with imports, usage and declarations of packages and functions. 问题中引用的R-devel新闻与现在的3-0分支NEWS文件有关,其中的特定条目列出了R CMD check将生成报告的特定情况,这些报告涉及导入,使用和声明包和函数的问题。

If you have questions regarding a particular warning the list is worth a look. 如果您对特定警告有疑问,则值得一看。

For further (and more in-depth) information see the wiki page for links to the Writing R Extensions R development guide and other helpful information. 有关更多(更深入)的信息,请参见 Wiki页面 ,该页面提供指向Writing R Extensions R开发指南和其他有用信息的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM