Scrape R script to create import and importFrom calls for roxygen2, namespace or description files

make_import(
  script,
  cut = NULL,
  print = TRUE,
  format = "oxygen",
  desc_loc = NULL
)

Arguments

script

character, connection to pass to readLines, can be file path, directory path, url path

cut

integer, number of functions to write as importFrom until switches to import, Default: NULL

print

boolean, print output to console, Default: TRUE

format

character, the output format must be in c('oxygen','description','import'), Default: 'oxygen'

desc_loc

character, path to DESCRIPTION file, if not NULL then the Imports fields in the DESCRIPTION file, Default: NULL

Examples


# copy dummy package to tempdir
file.copy(system.file('pkg',package = 'sinew'),tempdir(),recursive = TRUE)
#> [1] TRUE

pkg_dir <- file.path(tempdir(),'pkg')
pkg_dir_R <- file.path(pkg_dir,'R')
pkg_dir_DESC <- file.path(pkg_dir,'DESCRIPTION')

# update namespaces in package functions
pretty_namespace(pkg_dir_R, overwrite = TRUE)
#> 
#> functions changed in '/tmp/Rtmp9MrrZZ/pkg/R/yy.R':
#> 
#> ✔: found, : not found, (): instances, ☒: user intervention
#> 
#>  ✔ utils::head  (1) 
#>  ✔ stats::runif (1) 
#> 
#> 
#> functions changed in '/tmp/Rtmp9MrrZZ/pkg/R/zz.R':
#> 
#> ✔: found, : not found, (): instances, ☒: user intervention
#> 
#>  ✔ utils::head  (1) 
#>  ✔ stats::runif (1) 
#> 

# update imports/importsFrom for roxygen2 tags
make_import(pkg_dir_R,format = 'oxygen')
#>  
#> /tmp/Rtmp9MrrZZ/pkg/R/yy.R
#> #' @importFrom utils head
#> #' @importFrom stats runif
#>  
#> /tmp/Rtmp9MrrZZ/pkg/R/zz.R
#> #' @importFrom utils head
#> #' @importFrom stats runif

# update Imports for DESCRIPTION file output to console
make_import(pkg_dir_R,format = 'description')
#> Imports:
#> 	stats,
#> 	utils

# update Imports for DESCRIPTION file overwrite file
make_import(pkg_dir_R,format = 'description', desc_loc = pkg_dir)
#> Imports:
#> 	stats,
#> 	utils

cat(readLines(pkg_dir_DESC),sep = '\n')
#> Package: pkg
#> Title: What the Package Does (One Line, Title Case)
#> Version: 0.0.0.9000
#> Authors@R: person(given = "First", family = "Last", role = c("aut",
#>         "cre"), email = "first.last@example.com", comment = c(ORCID =
#>         "YOUR-ORCID-ID"))
#> Description: What the package does (one paragraph).
#> License: MIT + file LICENSE
#> Encoding: UTF-8
#> LazyData: true
#> Roxygen: list(markdown = TRUE)
#> RoxygenNote: 6.1.1
#> Imports: stats,
#> 	utils

# cleanup tempdir
unlink(pkg_dir, force = TRUE, recursive = TRUE)