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

3

24

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
Ramekin answered 12/5, 2016 at 17:17 Comment(0)
C
3

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.

Convincing answered 2/1, 2019 at 20:1 Comment(0)
T
2

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
Tibbitts answered 5/9, 2017 at 13:53 Comment(0)
D
2

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
Dunois answered 9/1, 2018 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.