say_hello = function(name = "my little friend", punct = ".")
{
cat( paste0("Hello, ", name, punct), "\n", sep="")
}
say_hello()Hello, my little friend.
2025-05-27
?A central difference between scripts and packages is structured documentation integrated into the R commandline.
Example: ?abs or help(abs) gives a detailed description of two arithmetic functions, with a reference, links to related subjects, and examples (to be run as example(abs))
Writing documentation is your responsibility. The standard format is the R documentation format. .Rd-files live in the man-folder of the source package. R builds boths interactive HTML and static PDF from the source.
This is what the .Rd file for abs looks like: MathFun.Rd
.Rd-format uses markup similar to LaTeX to define fields (name, title) and sections (description); it allows lists, links and some formatting, see Hadley’s R package book.
.RdLet’s start with a stupid function:
say_hello = function(name = "my little friend", punct = ".")
{
cat( paste0("Hello, ", name, punct), "\n", sep="")
}
say_hello()Hello, my little friend.
Let’s build a simple package in the working directory:
Problems with .Rd files:
Solution: package roxygen2
\@ directly in source .R filedocument() to translate to .Rd file(s) during buildingThis what our example could look like:
roxygen2 support is built into devtools, so we simply update the documentation like this:
Note: text formatting, links, lists etc. work exactly as in .Rd
@export tagroxygen2 solves the problem of having separate .Rd files for documentation.
roxygen2 also solves the problem of having a separate NAMESPACE file:
exportPatterndocument() will add all functions with the @export tag to NAMESPACE:::)Error messages like this are common when installing from devtools:
Error in fetch(key) : lazy-load database 'C:/Users/aleplo/Documents/R/R-3.6.1/library/say/help/say.rdb' is corrupt
Restart R to get rid of them.
A roxygen2-style documentation header is often useful for functions in scripts:
Frequent recommendation for writing good documentation:
Make the introduction short (1-5 lines).
Avoid long argument descriptions move extra material to Details.
Describe the return value; make use of itemize/describe for structured return values (e.g. data frames).
Use examples to demonstrate common use cases; examples should self-contained, using built-in datasets or simulated data.
Consider vignettes for complex use cases.
If you find it very hard to document your function(s), you may want to re-think your function- or package design.
R documentation mixes reference and tutorial, but is closer to a reference. It is often awkward for describing
background and motivation,
multiple or complex use cases,
use cases involving multiple functions.
Package-level documentation can help (see below), but these things are generally better in a vignette.
helpAny files in inst/doc in the source package will be copied to doc in the built/installed package: any format, any source.
A package vignette is a PDF or HTML document in doc in the built package generated from plain text literate source files (.Rmd, .Rnw) folder vignettes in the source package.
Multiple vignettes are possible, any content, but the main idea is tutorial or background rather than reference, e.g. browseVignettes(package = "survival").
Unless a package is extremely simple (a one-function package), write a vignette.
Generate a vignette template for our silly example:
This creates a new sub-directory in the package folder called vignettes and puts a template .Rmd file there.
Vignettes are NOT built by default when using install, you need to insist:
It can be helpful to have a central help page for the package as a whole. E.g.
roxygen can do this, too:
"_PACKAGE"\@docType packageCommonly this header is put into a separate .R file called <package-name>.R., but any R file will do.
This is what a package documentation for our package could look like: say.R
If your package cannot use a standard data set, include your own.
Data sets live in directory data of the package. Technically, data can be stored as a text file, R code that will generate data, or an .rda file. The latter is most common, recommended and well supported by devtools:
generates file data/names.rda with the single vector names. A local copy can be generated via
Data sets also need to be documented. This is again simple using roxygen, by including the data name with a roxygen header in one of the package .R files:
#' First names
#'
#' A vector with four common English first names.
#'
#' @format A character vector of length four.
#' @source Synthetic data set
"names"Documentation for data sets is built as before:
Comments:
Command from Hadley: Never @export a data set.
You can use @example, too.
Sometimes an external data set needs clean-up for inclusion in a package.
You can do this at the command line, and call use_data. However, for reasons of
you really should do it in an R script.
You could keep the script in data, in which case it will be run every time you load, build or install: that is probably overkill, and may be slow.
Instead:
data-raw to the source package with code and raw data.Rbuildignore)This is set up by calling use_data_raw:
which sets up the directory, puts a script template with the given name there, updates .Rbuildignore.
Your job is to copy the data file into data-raw, write the pre-processing code, and run the script once to build the .rda file.
Of course, you still have to document the data.
pkgdown to build a website for your packageExample: causaloptim