Populating Imports

library(sinew)

When you are building a package to submit to CRAN and you need to have namespace calls for any function that is being imported. It is a pain to manually parse through the code looking for all the :: and writing it in the roxygen2 header. This function does that for you.

You can write normally your script with the namespace calls and in the end run the function and you can paste the output into the header. (or use it as part of sinew::makeOxygen or sinew::makeOxyFile)

The function is written to work on single files or whole directories, like a package R subdirectory.

The output can be set to return the format needed for either an roxygen2 header or the DESCRIPTION

Package Setup

pkg_dir <- file.path(tempdir(),'pkg')
pkg_dir_r <- file.path(pkg_dir, 'R')

usethis::create_package(path = pkg_dir, open = FALSE)
#> ✓ Creating '/var/folders/kx/t4h_mm1910sb7vhm_gnfnx2c0000gn/T/Rtmp79mfWP/pkg/'
#> ✓ Setting active project to '/private/var/folders/kx/t4h_mm1910sb7vhm_gnfnx2c0000gn/T/Rtmp79mfWP/pkg'
#> ✓ Creating 'R/'
#> ✓ Writing 'DESCRIPTION'
#> ✓ Writing 'NAMESPACE'
#> ✓ Setting active project to '<no active project>'
withr::with_dir(pkg_dir, usethis::use_data_raw(open = FALSE))
#> ✓ Setting active project to '/private/var/folders/kx/t4h_mm1910sb7vhm_gnfnx2c0000gn/T/Rtmp79mfWP/pkg'
#> ✓ Creating 'data-raw/'
#> ✓ Adding '^data-raw$' to '.Rbuildignore'
#> ✓ Writing 'data-raw/DATASET.R'
#> • Finish the data preparation script in 'data-raw/DATASET.R'
#> • Use `usethis::use_data()` to add prepared data to package
withr::with_dir(pkg_dir, usethis::use_mit_license(copyright_holder = "John Doe"))
#> ✓ Setting License field in DESCRIPTION to 'MIT + file LICENSE'
#> ✓ Writing 'LICENSE'
#> ✓ Writing 'LICENSE.md'
#> ✓ Adding '^LICENSE\\.md$' to '.Rbuildignore'
withr::with_dir(pkg_dir, usethis::use_roxygen_md())
example_file <- system.file('example.R', package = 'sinew')
untangle(
  file = example_file, 
  dir.out = pkg_dir_r, 
  dir.body = file.path(pkg_dir, 'data-raw')
)
pretty_namespace(pkg_dir_r,overwrite = TRUE)

DESCRIPTION

make_import(script = pkg_dir_r,format = 'description')
#> Imports:
#>  stats,
#>  utils

To write the output directly into the Imports field of the DESCRIPTION file, specify path to DESCRIPTION in desc_loc

sinew::update_desc(path = pkg_dir_r, overwrite = TRUE)
Click to see DESCRIPTION file

Package: pkg
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R: person("First", "Last", , "first.last@example.com", role =
        c("aut", "cre"), comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
Imports: stats, utils


roxygen2

#single file
make_import(script = file.path(pkg_dir_r,'yy.R') ,format = 'oxygen')
#>  
#> /var/folders/kx/t4h_mm1910sb7vhm_gnfnx2c0000gn/T//Rtmp79mfWP/pkg/R/yy.R
#> #' @importFrom utils head
#> #' @importFrom stats runif
#whole directory
make_import(script = pkg_dir_r,format = 'oxygen')
#>  
#> /var/folders/kx/t4h_mm1910sb7vhm_gnfnx2c0000gn/T//Rtmp79mfWP/pkg/R/yy.R
#> #' @importFrom utils head
#> #' @importFrom stats runif
#>  
#> /var/folders/kx/t4h_mm1910sb7vhm_gnfnx2c0000gn/T//Rtmp79mfWP/pkg/R/zz.R
#> #' @importFrom utils head
#> #' @importFrom stats runif

Cutoff

Setting the parameter cut to an integer value allows for control of how many functions to list in a package before concatenating the importFrom to an import. This is useful when there are many functions being used throughout the package from the same library and it is practically the same as just importing the whole library

#with cut
make_import(script=file.path(pkg_dir_r,'yy.R'),format = 'oxygen', cut = 1)
#>  
#> /var/folders/kx/t4h_mm1910sb7vhm_gnfnx2c0000gn/T//Rtmp79mfWP/pkg/R/yy.R
#> #' @import utils
#> #' @import stats

Cleanup

unlink(pkg_dir, recursive = TRUE, force = TRUE)