Populating Imports

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
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)
withr::with_dir(pkg_dir, usethis::use_data_raw(open = FALSE))
withr::with_dir(pkg_dir, usethis::use_mit_license(copyright_holder = "John Doe"))
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')

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)
details::details(file.path(pkg_dir,'DESCRIPTION'), summary = 'Click to see DESCRIPTION file',lang = '')

roxygen2

#single file
make_import(script = file.path(pkg_dir_r,'yy.R') ,format = 'oxygen')
#whole directory
make_import(script = pkg_dir_r,format = 'oxygen')

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)

Cleanup

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


Try the sinew package in your browser

Any scripts or data that you put into this service are public.

sinew documentation built on March 31, 2022, 5:06 p.m.