openstatsware
short course: Good Software Engineering Practice for R Packages
August 24, 2025
Any opinions expressed in this presentation and on the following slides are solely those of the presenter and not necessarily those of their employers.
install.packages()
is used to install packages into the librarylibrary()
is used to load and attach packages from the library
search
list — objects in the package can be used directlyPackage source = directory with files and subdirectories
Once upon a time, developers would set up this structure manually 🥱
Nowadays, it’s super fast with:
usethis::create_package()
DESCRIPTION
fileMajor.Minor.Patch
syntax (Semantic Versioning)DESCRIPTION
file (cont’d)library
your packageroxygen2
), running examples, tests (testthat
), vignettesR
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
skeleton with Code > Insert Roxygen Skeletonroxygen2
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/my_sum.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 compilersvignettes
folderpdf
or html
) created by compiling source filesusethis::use_vignette()
Rmd
vignette, compiled with knitr
roxygen2
chunksNEWS
fileusethis::use_news_md()
DESCRIPTION
fileLicense
field (in standardized form) is mandatoryCommon licenses for R packages: GPL-2, GPL-3, AGPL-3, MIT, …
Details and description of most licenses are available online:
Examples of standardized license specifications:
License: GPL-2
License: LGPL (>= 2.0, < 3) | Mozilla Public License
License: GPL-2 | file LICENCE
License: Artistic-2.0 | AGPL-3 + file LICENSE
LICENSE
/LICENCE
contains a copy of the license
License
fieldOnce again, functions from the usethis
package simplify this process:
usethis::use_mit_license()
usethis::use_gpl_license()
usethis::use_agpl_license()
usethis::use_lgpl_license()
usethis::use_apache_license()
usethis::use_cc0_license()
usethis::use_ccby_license()
usethis::use_proprietary_license()
NAMESPACE
devtools::document()
R CMD check
devtools::check()
devtools::build()
R CMD INSTALL
devtools::install()
devtools::load_all()
tests
will also be availableDescription | Windows & Linux | Mac |
---|---|---|
Install and Restart | Ctrl+Shift+B | Shift+Command+B |
Load All | Ctrl+Shift+L | Shift+Command+L |
Test Package | Ctrl+Shift+T | Shift+Command+T |
Check Package | Ctrl+Shift+E | Shift+Command+E |
Document Package | Ctrl+Shift+D | Shift+Command+D |
DESCRIPTION
fileroxygen2
documentation\(\leadsto\) We will be using this package throughout the day!
In the current version, changes were done by (later authors): Liming Li , Philippe Boileau , Alessandro Gasparini
This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
The source files are hosted at github.com/openstatsware/shortcourse-iscb2025, which is forked from the original version at github.com/RCONIS/workshop-r-swe-zrh.
Important: to use this work you must provide the name of the creators (initial authors), a link to the material, a link to the license, and indicate if changes were made.