简体   繁体   English

R包:描述,选择性导入和命名空间

[英]R package: description, selective import and namespace

Although there are quite a few postings on similar topics, none of them helped me understanding how to setup the DESCRIPTION file an R package. 尽管在类似主题上有很多帖子,但它们都没有帮助我理解如何将描述文件设置为R包。

My questions are: 我的问题是:

1.) Is my description file correct now? 1.)我的描述文件现在是否正确? Did I use "depends" and "imports" correctly? 我是否正确使用“依赖”和“导入”? (maybe duplicate question...) (也许是重复的问题...)

2.) Are required packages (dependencies?) automatically installed along with my package when needed, or "loaded" when one of my package function needs to refer to a function of an imported package? 2.)需要的包(依赖项?)是否在需要时自动与我的包一起安装,或者当我的某个包函数需要引用导入包的功能时“加载”? (didn't find anything on this issue yet...) (在这个问题上没找到任何东西......)

I tried to submit a package to CRAN and got following feedback: 我试图向CRAN提交一个包并得到以下反馈:

  • checking package dependencies ... NOTE Depends: includes the non-default packages: 'MASS' 'car' 'foreign' 'ggplot2' 'lmtest' 'plyr' 'reshape2' 'scales' Adding so many packages to the search path is excessive and importing selectively is preferable. 检查包依赖...注意取决于:包括非默认包:'MASS''car''外来''ggplot2''lmtest''plyr''reshape2''scale'在搜索路径中添加这么多包过多有选择地进口是优选的。

I originally had listed all above mentioned packages in the depends section of the DESCRIPTION file. 我最初在DESCRIPTION文件的depends部分列出了上面提到的所有包。 In the NAMESPACE file, I used import(pkgName) for all packages listed above. 在NAMESPACE文件中,我对上面列出的所有包使用了import(pkgName)

After that, I updated my files using importFrom(pkgName, function) in the NAMESPACE file and moved most of the packages to the imports section of my DESCRIPTION file. 之后,我使用NAMESPACE文件中的importFrom(pkgName, function)更新了我的文件,并将大部分包移到了DESCRIPTION文件的导入部分。 The package check with the current R-devel-version no longer gives this note. 使用当前R-devel版本的包检查不再提供此注释。 Here's an extract of my DESCRIPTION file: 这是我的DESCRIPTION文件的摘录:

License: GPL-3
Depends:
    ggplot2
Imports:
    MASS,
    car,
    foreign,
    lmtest,
    plyr,
    reshape2,
    scales
Collate:
    'sjImportSPSS.R'

and the NAMESPACE file: 和NAMESPACE文件:

import(ggplot2)
importFrom(MASS,lda)
importFrom(MASS,loglm)
importFrom(car,crPlots)
importFrom(car,durbinWatsonTest)
importFrom(car,influencePlot)
importFrom(car,leveragePlots)
importFrom(car,ncvTest)
importFrom(car,outlierTest)
importFrom(car,spreadLevelPlot)
importFrom(car,vif)
importFrom(foreign,read.spss)
importFrom(lmtest,bptest)
importFrom(plyr,adply)
importFrom(plyr,ddply)
importFrom(reshape2,melt)
importFrom(scales,brewer_pal)
importFrom(scales,percent)

I'm unsure whether this approach addresses the issue given in the check note above. 我不确定这种方法是否解决了上面的检查说明中给出的问题。 Furthermore, when I load my package with library(sjPlot) , ggplot2 is also attached, but none of the other packages. 此外,当我用library(sjPlot)加载我的包时,ggplot2也被附加,但没有其他包。 Does my package still work for other users? 我的包裹是否仍适用于其他用户? What if they don't have all needed packages installed? 如果他们没有安装所有需要的软件包怎么办?

From ?install.packages the default behavior is that Depends: and Imports: packages are installed if not already installed. ?install.packages ,默认行为是Depends:和Imports:如果尚未安装,则安装包。 Check out sessionInfo() and you'll see your Imports: are loaded (resident in memory) but not attached (available on disk). 检查sessionInfo() ,您将看到您的Imports:已加载(驻留在内存中)但未附加(在磁盘上可用)。 If your importFrom statements cover the symbols used in your package code, then your code will work for others (if there were missing imports, you would be warned about undefined global variables). 如果您的importFrom语句涵盖了包代码中使用的符号,那么您的代码将适用于其他符号(如果缺少导入,则会向您发出有关未定义的全局变量的警告)。

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

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