R package development workshop
June 27, 2027
R CMD check
is the command line utility provided by R to check R packages.
It checks that all the components are valid and consistent with each other, in particular:
.Rd
files in /man
It will also run any examples and tests you have written.
devtools::check()
will run R CMD check
in the background, with the results shown in the Build pane.
devtools::check()
You will get lots of output. It will end with:
-- R CMD check results ---------- animalsounds 0.0.0.9000 ----
Duration: 9.3s
> checking DESCRIPTION meta-information ... WARNING
Non-standard license specification:
`use_mit_license()`, `use_gpl3_license()` or friends to pick a
license
Standardizable: FALSE
0 errors √ | 1 warnings x | 0 notes √
We haven’t yet specified a license for our package.
On running devtools::check()
you may get an error of the form
Updating animalsounds documentation
Error: The specified file is not readable: path-to\animalsounds\NAMESPACE
This can happen if your repo is on a networked drive.
This is covered in this Stackoverflow question and can be fixed.
Save a copy of this file: fix_for_networked_drives.R
Save it somewhere other than the animalsounds
directory
Open the file from the animalsounds
project session
Run the whole file
You should now find that devtools::check()
proceeds normally.
ERROR | Must fix to get the code/example/test working |
WARNING | Fix if sharing with others |
NOTE | Fix if submitting to CRAN |
It is possible to submit to CRAN with a NOTE, but it’s best avoided.
DESCRIPTION
changer
package can help you change the name!usethis::use_version()
DESCRIPTION
Authors@R: A call to person()
that is run to create the Author field when the package tarball is built. “aut” means author, “cre” means creator (maintainer), “ctb” means contributor.
A placeholder call to person()
is inserted in DESCRIPTION
when a package is created with usethis::create_package()
which can be edited directly:
Alternatively, this can be overwritten with a call to usethis::use_author()
:
DESCRIPTION
true
data sets in the package are stored in a database during package installation and loaded from the database as required. Recommended if shipping data with package – usethis::use_data()
will set this for you.There are three main open source licenses:
CC0: “public domain”, best for data packages.
MIT: Free for anyone to do anything with (including bundling in closed source product).
GPL: Changes and bundles must also be GPL
If you are including someone else’s GPL code directly, you must use GPL yourself.
You can use usethis::use_proprietary()
to make it clear that your package isn’t open source.
In DESCRIPTION:
License: file LICENSE
In LICENSE:
Copyright 2023 ACME Ltd. All rights reserved.
This slide is specific for The University of Warwick, but similar considerations are likely to be true at other Universities.
Software is defined as a creative output (unlike scholarly works, e.g. thesis, conference paper)
The university owns the IP of any software created by Warwick PhD students and staff in the course of their work
Before making code public or publishing software under any license, contact Brendan at B.Spillane@warwick.ac.uk
Tip
In RStudio, you can use the ‘Go to file/function’ box or Ctrl + . [period] and start typing a file name to open a file for editing.
The README is the quick start guide to your package.
It should include
You should be able to borrow from the DESCRIPTION and help files!
It’s readable on the package’s GitHub repo and on the homepage of its website (more on that later).
usethis has functions to set up a README with/without R code
README.Rmd
must be rendered to make README.md
each time it is changed.
usethis::use_readme_rmd()
creates a pre-commit hook to check if README.Rmd
and README.md
are out of sync before committing.
Use build_readme()
to render with the latest version of the code.
GitHub Actions (GHAs) allow you to run code every time you push to GitHub.
The most useful ones for packages can be selected from a call to usethis::use_github_action()
:
Which action do you want to add? (0 to exit)
(See <https://github.com/r-lib/actions/tree/v2/examples> for other options)
1: check-standard: Run `R CMD check` on Linux, macOS, and Windows
2: test-coverage: Compute test coverage and report to https://about.codecov.io
3: pr-commands: Add /document and /style commands for pull requests
check-standard
sets up a GHA that runs R CMD check
with the latest release of R on Linux, Mac, and Windows and with both the previous release and development release of R on Linux.
use_github_action("check-release")
The check-standard
GHA is best-practice for ‘serious’ projects, e.g. those aiming for CRAN, but is overkill for our purposes.
We can set up a simpler GHA by specifying an alternative:
This sets up a bare-minimum workflow that runs R CMD check
with the latest release of R on Linux.
It’s good for simple package with no OS-sepcific code, and if you want a quick start with R CI.
animalsounds
with usethis::use_readme_rmd()
.build_readme()
, then add both README.Rmd
and README.md
in a git commit.usethis::use_github_action("check-release")
. It adds a badge to the README, so you will need to render the README again.Vignettes are long-form documentation for your package.
They use R markdown to integrate code and output into the documentation. Typically:
A vignette with the same name as the package (e.g., vignettes/animalsounds.Rmd
or vignettes/articles/animalsounds.Rmd
) automatically becomes a top-level “Get started” link.
use_vignette()
Easiest way to get started is with usethis::use_vignette()
Adds to DESCRIPTION
Suggests: knitr VignetteBuilder: knitr
Creates vignettes/
Drafts vignettes/name.Rmd
---
title: "Vignette Title"
author: "Vignette author"
date: "2024-06-13"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
html_vignette
output uses a custom style sheet to keep the file size of the HTML as small as possible.vignette:
field contains special metadata needed when the package is built. Don’t forget to change the title here too!Vignette code needs to be able to run on CRAN. There are some cases where this is not possible.
As an alternative, you can use articles instead of vignettes:
✔ Adding 'rmarkdown' to Config/Needs/website
✔ Creating 'vignettes/'
✔ Creating 'vignettes/articles/'
✔ Adding '*.html', '*.R' to 'vignettes/.gitignore'
✔ Writing 'vignettes/articles/my-article.Rmd'
• Modify 'vignettes/articles/my-article.Rmd'
✔ Adding '^vignettes/articles$' to '.Rbuildignore'
animalsounds
, that shows how to use animal_sounds()
.devtools::install(build_vignettes = TRUE)
to install the package with the vignettes. Call browseVignettes("animalsounds")
to open your vignette.Add news for the latest version at the top.
Use a top-level heading for each release version
Add each change in a bulleted list:
## Major changes
, ## Minor improvements
, ## Bug fixes
).Note connections to GitHub:
#10
).#101, @hadley
).The pkgdown package is designed to make it quick and easy to build a website for your package:
Why have a website for your package?
pkgdown::build_site()
creates a package website based on the standard documentation files.
Home page: based on README
Reference:
Articles: one page for each vignette
Get Started: if you have a vignette with filename = package name
News: based on NEWS.md
Plus:
You can host your website directly from its GitHub repo (repo has to be public)
The recommended approach is to let GitHub build your page (instead of calling pkgdown::build_site()
and committing and pushing the artifacts of the built website (i.e., html files) to GitHub
Add an action to your GitHub repo to be run automatically every time you push to it to rebuild the site:
The URL will be https://USERNAME.github.io/animalsounds
You can add more information to _pkgdown.yml
to customise the package website:
curate the index for the Reference page - functions can be grouped and described in categories
customise the appearance
usethis::use_pkgdown_github_pages()
– this will ask you to install pkgdown if you don’t already have it.Wickham, H and Bryan, J, R Packages (2nd edn, in progress), https://r-pkgs.org.
R Core Team, Writing R Extensions, https://cran.r-project.org/doc/manuals/r-release/R-exts.html
Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0).