Good Software Engineering Practice for R Packages
July 20, 2023
install.packages() and R CMD INSTALL is used to install packages into the library.library() is used to load and attach packages from the library.
search list (objects in the package can be used directly).Package source = directory with files and subdirectories.
Once upon a time, developers would set up this structure manually. š„±
Nowadays, it is super fast with:
usethis::create_package()DESCRIPTION FileMajor.Minor.Patch syntaxDESCRIPTION File (contād)library your package.roxygen2), running examples, tests (testthat), vignettes.R Folder.R suffix)
usethis::use_r("filename")require(), options() etc.Collate field of DESCRIPTION automatically).NAMESPACE FileNAMESPACE File (contād)search() pathman Folder.Rd format
LaTeX.Rd files and the NAMESPACE manually. š„±roxygen2! šroxygen2 to the Rescue!#' and special macros preceded with @.Rd files and the NAMESPACE file for youusethis::use_roxygen_md()roxygen2 in your projectroxygen2 SourceR/my_sum.R:
#' My Summation Function
#'
#' This is my first function and it sums two numbers.
#'
#' @param x first summand.
#' @param y second summand.
#'
#' @return The sum of `x` and `y`.
#' @export
#'
#' @note This function is a bit boring but that is ok.
#' @seealso [Arithmetic] for an easier way.
#'
#' @examples
#' my_sum(1, 2)
my_sum <- function(x, y) {
x + y
}roxygen2 Outputman/my_sum.Rd:
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/bla.R
\name{my_sum}
\alias{my_sum}
\title{My Summation Function}
\usage{
my_sum(x, y)
}
\arguments{
\item{x}{first summand.}
\item{y}{second summand.}
}
\value{
The sum of \code{x} and \code{y}.
}
\description{
This is my first function and it sums two numbers.
}
\note{
This function is a bit boring but that is ok.
}
\examples{
my_sum(1, 2)
}
\seealso{
\link{Arithmetic} for an easier way.
}roxygen2 Output (contād)NAMESPACE:
tests Folderusethis::use_testthat() and usethis::use_test() and populate tests/testthat folder with unit teststestthat framework, then these can go into R scripts directly in tests directorydata Folderusethis::use_data()data() call needed before using the datadata-raw folder, start with usethis::use_data_raw()inst Folderinst/extdata foldersystem.file("path/file", package = "mypackage")CITATION: For custom citation() output
usethis::use_citation()inst/doc can contain documentation files (typically pdf)src FolderC, C++ and Fortran usually works with OS native compilersRcpp::Rcpp.package.skeleton()vignettes Folderpdf or html) created by compiling source filesusethis::use_vignette()
Rmd vignette, compiled with knitrroxygen2 chunksNEWS Fileusethis::use_news_md()NAMESPACER CMD checkR CMD INSTALLdevtools::load_all()tests will also be availableDESCRIPTION file