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:
roxygen2
) andI 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.
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
Regarding the two bullets you wonder about at the bottom:
devtools
package for updating the DESCRIPTION file, instead of adding this to roxygen2
. So in that sense, devtools
would be the first available choiceIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With