Use create_package
1 Create a package or project from the usethis
2 usethis is a workflow package package.
usethis::create_package("<name>")
R code goes in the R
directory - create manually or via use_r
3 Create or edit R or test files.
usethis::use_r("<name>")
Rcpp code goes in the src
directory - create manually or via use_rcpp
, use_rcpp_armadillo
, or use_rcpp_eigen
4 Use C, C++, RcppArmadillo, or RcppEigen.
usethis::use_rcpp("<name>")
Generate RcppExports.cpp
via Rcpp::compileAttributes
5 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()
Use the MIT (or another) license6 License a package.
usethis::use_mit_license(name = "<name>")
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 //'
.
Package documentation is generated from inline Roxygen documentation using the document
9 Use roxygen to document a package function:
devtools::document()
Checking a packages for potential problems (warnings/errors/notes) can be performed via the check
10 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 ✔
You can load all functions into the current R session (including internal functions) via load_all
11 Load complete package:
devtools::load_all()
The package can be installed via install
/uninstall
12 Install a local development package / uninstall
13 Uninstall a local development package:
devtools::install()
devtools::uninstall()