Extract scripts dependencies and generate your Description file

Sébastien Rochette

2020-03-15

Load package {attachment}

library(attachment)

Use “devstuff_history.R”

When building your package, create a file called “devstuff_history.R” in the root directory. You will store all “manual” calls to devtools::xxx and usethis::xxx in this script.
Its first line should be :

usethis::use_build_ignore("devstuff_history.R")

You can then call {attachment} in this file to help you build your description file.

Fill your DESCRIPTION file

For packages

What you really want is to fill and update your description file along with the modifications of your documentation. Indeed, only this function will really be called in your “devstuff_history.R”.
Run attachment::att_amend_desc() each time before devtools::check(), this will save you some warnings and errors !

att_amend_desc()

For bookdown

You can use a similar approch for a {bookdown} description file using attachment::att_to_desc_from_is()

# bookdown Imports are in Rmds
imports <- c("bookdown", attachment::att_from_rmds("."))
attachment::att_to_desc_from_is(path.d = "DESCRIPTION",
                                imports = imports, suggests = NULL)

Get all packages listed in “namespace”

You can get the list of packages in your package with att_from_namespace()

att_from_namespace()

Get all packages added using pkg::function or library/require

This reads all files in directories of R scripts (default to R directory of a package)

att_from_rscripts()

Get all packages called in your Rmd

If you have vignette, you may want to list extra libraries, not listed in your “Depends” list. This function applies to any Rmd file, of course.

att_from_rmds()

Create a file for package installation

Once your package is finished. Well, is a package ever finished ? Let’s say, once you want to release a version of your package, you may want to deliver the list of dependencies your users will have to install. A little script like install.packages(c(...all dep...)) would be so nice :

create_dependencies_file()

This file will be placed in inst/dependencies.R and contains :

# No Remotes ----
# remotes::install_github("ThinkR-open/fcuk")
# Attachments ----
to_install <- c("covr", "desc", "devtools", "glue", "knitr", "magrittr", "rmarkdown", "stats", "stringr", "testthat", "utils")
for (i in to_install) {
  message(paste("looking for ", i))
  if (!requireNamespace(i)) {
    message(paste("     installing", i))
    install.packages(i)
  }
}

Other possibilities

Of course, you can also use {attachment} out of a package to list all package dependencies of R scripts using att_from_rscripts or Rmd files using att_from_rmds.

dummypackage <- system.file("dummypackage", package = "attachment")

att_from_rscripts(path = file.path(dummypackage, "R"))
#> [1] "stats"
att_from_rmds(path = file.path(dummypackage, "vignettes"))
#> [1] "knitr"     "rmarkdown" "ggplot2"