Package dependencies for R base packages [duplicate]
Asked Answered
B

1

9

When writing a package in R, is it necessary to add base packages (utils, grDevices, stats, etc.) as dependencies in the DESCRIPTION of your package?

Some packages do this, but most seem to not.

I have not found any information in the Writing R Extensions manual regarding this.

Burra answered 11/9, 2016 at 17:54 Comment(6)
Yes, you should Import them in DESCRIPTION and NAMESPACE, as needed. Older packages may not have complied with this policy as it was only implemented on CRAN last year.Unroot
@Unroot the R CMD check did not care about this when I ran it. Also, please show me where this issue is dealt with in the CRAN policies because I could not find it there. (Note that my question applies selectively to base packages in R.) As a side note I do not see how your proposed duplicate deals with this issue as CRAN gives me no note, warning, or error when I run R CMD.Burra
Run R CMD check --as-cran. This is not super explicit but where WRE says "all packages" need to pass that, they also mean default packages other than base.Unroot
It passed the R CMD check on CRAN's servers that I ran using devtools::build_win(). Might it have something to do with me specifying R in Depends?Burra
Are you running this locally with the most recent version of R-devel? If you're putting packages in Depends (which you probably never want to do... use Imports and NAMESPACE instead), you won't get warnings about this. And you don't need Depends: R unless you specify a specific version.Unroot
I am not putting any packages in Depends apart from R. I only use Imports (and Suggests) and have only used NAMESPACE for exporting functions and :: to import all functions. I specified a version for R in depends. I don't see why running R-devel locally would be better than running it on the CRAN servers?Burra
D
1

You should not set too much dependencies but prefer to use those packages as import :

for instance in the DESCRIPTION you will write

     Imports:    
     graphics,
     utils,
     stats,
     grDevices

In your NAMESPACE you can then use either importFrom, in the case you only have a few functions to use. Then you don't have to point to the function using pkg::fun(), or import pkg which will import the whole package, and again you will not need to use the pkg::fun().

Below an example of what you can write in your NAMESPACE

    import(graphics)
    importFrom(stats,coef)
    importFrom(stats,ftable)
    importFrom(stats,na.fail)
    importFrom(utils,data)
    importFrom(utils,globalVariables)
    importFrom(utils,read.csv)
    importFrom(utils,select.list)
    importFrom(utils,stack)
    importFrom(utils,write.table)

If you try to use those functions without importing them or use depends, the R-CMD check will fail.

Deerstalker answered 11/9, 2016 at 18:59 Comment(7)
I have accessed all my functions using :: in the package but have not added any imports to NAMESPACE. The R CMD check, however, did not throw me any errors about not adding grDevices (for instance) as it usually does for packages that are not part of the base R.Burra
I think previous comments from @thomas are giving you the right answer. I had those warnings to cope with on R-forge, and before I simply used depends, then I had some warning that I had too much package in depends. Try R CMD CHECK --as-cran just like thomas said.Deerstalker
As I mentioned above @Deerstalker I get no warnings and the R CMD check has been run on the CRAN servers.Burra
I have checked that for you. I've removed one line from my NAMESPACE, importFrom(stats,sd). When running R-CMD Check --as-cran I get the following : * checking R code for possible problems ... NOTE funstatJournalier: no visible binding for global variable 'sd'. It is just a note but given this I will not be able to send the package to CRAN. I get this * using R version 3.3.1 (2016-06-21) * using platform: x86_64-w64-mingw32 (64-bit)Deerstalker
when I point to sd using stats::sd I don't get any error. But in Rexts manual it is said "Packages implicitly import the base namespace. Variables exported from other packages with namespaces need to be imported explicitly using the directives import and importFrom. " and "Using importFrom selectively rather than import is good practice and recommended". So I tend to stick to that adviceDeerstalker
This is why I posed the question in the first place. Perhaps I haven't been clear enough. This behavior is different compared to other packages that aren't part of the base installation. For example, adding lattice::dotplot(1:10) to the code (without importing lattice) will have theR CMD check return an error, but this doesn't happen with utils, graphics, stats, etc. The only way it makes sense to me is if there are R installations without some of these base packages, otherwise I can't see why importing them matters.Burra
I understand what you mean there but see stat for example is using import from graphics in its namespace importFrom(grDevices, as.graphicsAnnot, dev.cur, dev.flush, dev.hold, dev.interactive, dev.new, dev.set, devAskNewPage, extendrange, n2mfrow, palette, xy.coords)Deerstalker

© 2022 - 2024 — McMap. All rights reserved.