Packages

2020

Creation

Use create_package1 Create a package or project from the usethis2 usethis is a workflow package package.

usethis::create_package("<name>")

R code

R code goes in the R directory - create manually or via use_r3 Create or edit R or test files.

usethis::use_r("<name>")

Rcpp code

Rcpp code goes in the src directory - create manually or via use_rcpp, use_rcpp_armadillo, or use_rcpp_eigen4 Use C, C++, RcppArmadillo, or RcppEigen.

usethis::use_rcpp("<name>")

Generate RcppExports.cpp via Rcpp::compileAttributes5 compileAttributes: Scan the source files within a package for attributes and generate code as required. Generates the bindings required to call C++ functions from R for functions adorned with the ‘Rcpp::export’ attribute..

Rcpp::compileAttributes()

License

Use the MIT (or another) license6 License a package.

usethis::use_mit_license(name = "<name>")

Documentation

Roxygen7 roxygen2: In-Line Documentation for R comments can be added to R and Rcpp functions using tags8 R Packages: Roxygen comments, Introduction to roxygen2.

A simple template with some common parameters:

#' <Short description>
#'
#' <Long description>
#'
#' @param param_name <parameter description>
#' @return <description of returned object>
#' 
#' @examples
#' function_name(x)
#' 
#' @export
function_name <- function(param_name) {
    # do something
}

The same can be done for exported Rcpp functions by substituting #' for //'.

Updating documentation

Package documentation is generated from inline Roxygen documentation using the document9 Use roxygen to document a package function:

devtools::document()

Checking

Checking a packages for potential problems (warnings/errors/notes) can be performed via the check10 Build and check a package, cleaning up automatically on success function:

devtools::check()

If everything checks out, you should see:

0 errors ✔ | 0 warnings ✔ | 0 notes ✔

Loading functions

You can load all functions into the current R session (including internal functions) via load_all11 Load complete package:

devtools::load_all()

Installing

The package can be installed via install/uninstall12 Install a local development package / uninstall13 Uninstall a local development package:

devtools::install()
devtools::uninstall()