R/Pharma workshop: Good Software Engineering Practice for R Packages
October 28, 2024
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 directlyPlease enter in the chat:
Package 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
syntaxDESCRIPTION
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/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 compilersvignettes
Folderpdf
or html
) created by compiling source filesusethis::use_vignette()
Rmd
vignette, compiled with knitr
roxygen2
chunksNEWS
Fileusethis::use_news_md()
NAMESPACE
devtools::document()
R CMD check
devtools::check()
devtools::build()
R CMD INSTALL
devtools::install()
devtools::load_all()
tests
will also be availableDESCRIPTION
fileroxygen2
documentationIn the current version, changes were done by (later authors): Liming Li , Philippe Boileau
This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
The source files are hosted at github.com/openpharma/workshop-r-swe-rinpharma-2024, which is forked from the original version at github.com/RCONIS/user2024-tutorial-gswep.
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