Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R package: description, selective import and namespace

Tags:

package

r

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.

My questions are:

1.) Is my description file correct now? 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? (didn't find anything on this issue yet...)

I tried to submit a package to CRAN and got following feedback:

  • 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.

I originally had listed all above mentioned packages in the depends section of the DESCRIPTION file. In the NAMESPACE file, I used import(pkgName) for all packages listed above.

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. The package check with the current R-devel-version no longer gives this note. Here's an extract of my DESCRIPTION file:

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

and the NAMESPACE file:

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. Does my package still work for other users? What if they don't have all needed packages installed?

like image 882
Daniel Avatar asked Nov 28 '13 13:11

Daniel


People also ask

What is R namespace?

The NAMESPACE file allows R packages to communicate with one another. You can export your functions to make them available to others and import functions from other packages if you use them in your code. roxygen2 can be used to to export and import functions.

What are package dependencies in R?

A dependency is a code that your package needs to run. Dependencies are managed by two files. The DESCRIPTION manages dependencies at the package level; i.e. what packages needs to be installed for your package to work. R has a rich set of ways to describe different types of dependencies.


1 Answers

From ?install.packages the default behavior is that Depends: and Imports: packages are installed if not already installed. Check out sessionInfo() and you'll see your Imports: are loaded (resident in memory) but not attached (available on disk). 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).

like image 173
Martin Morgan Avatar answered Sep 21 '22 21:09

Martin Morgan