Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why roxygen2 does not automatically update "Imports" in DESCRIPTION file?

Tags:

r

roxygen2

I am trying to follow closely @hadley's book to learn best practices in writing R packages. And I was thrilled to read these lines about the philosophy of the book:

anything that can be automated, should be automated. Do as little as possible by hand. Do as much as possible with functions.

So when I was reading about dependencies and the (sort of) confusing differences between import directives in the NAMESPACE file and the "Imports:" field in the DESCRIPTION file, I was hoping that roxygen2 would automatically handle both of them. After all

Every package mentioned in NAMESPACE must also be present in the Imports or Depends fields.

I was hoping that roxygen2 would take every @import in my functions and make sure it is included in the DESCRIPTION file. But it does not do that automatically.

So I either have to add it manually to the DESCRIPTION file or almost manually using devtools::use_package.

Looking around for an answer, I found this question in SO, where @hadley confirms in the comments that

Currently, the namespace roclet will modify NAMESPACE but not DESCRIPTION

and other posts (e.g. here or here) where collate_roclet is discussed, but "This only matters if your code has side-effects; most commonly because you’re using S4".

I wonder:

  • the reason that DESCRIPTION is not automatically updated (sort of contradicting the aforementioned philosophy, which is presumably shared by roxygen2) and
  • If someone has already crafted a way to do it
like image 873
elikesprogramming Avatar asked May 12 '16 17:05

elikesprogramming


3 Answers

I have written a little R package for that task:

https://github.com/markusdumke/pkghelper

It scans the R Code and NAMESPACE for packages in use and adds them to the Imports section.

like image 50
needRhelp Avatar answered Nov 04 '22 17:11

needRhelp


The namespace_roclet edits the NAMESPACE file based on the tags added in the script before the function. As there are three types of dependencies (Depends, Imports, and Suggests), a similar method as used by the namespace_roclet would require three different tags (notice Imports should be a different one, to differentiate it from the packages to attach in NAMESPACE).

If you are willing to take a semi-automated process, you could identify the packages you have used and add the missing ones to DESCRIPTION, in the adequate sections.

library(reinstallr)

package.dir <- getwd()
base_path   <- normalizePath(package.dir)
files       <- list.files(file.path(base_path, "R"), full.names = TRUE)
packages    <- unique(reinstallr:::scan_for_packages(files)$package)

packages
like image 2
Francisco Marco-Serrano Avatar answered Nov 04 '22 15:11

Francisco Marco-Serrano


Regarding the two bullets you wonder about at the bottom:

  • Updates to the DESCRIPTION file could be further automated with additional roclets, however already >4 years ago such a pull request was deferred: https://github.com/klutometis/roxygen/pull/76
  • I have to assume that the guys would indeed rather have you use the devtools package for updating the DESCRIPTION file, instead of adding this to roxygen2. So in that sense, devtools would be the first available choice
like image 2
RolandASc Avatar answered Nov 04 '22 16:11

RolandASc