Skip to content

R Package Development Workflow

Dimi edited this page Jul 6, 2021 · 1 revision

Typical workflow

When developing a new function

  1. write a test (optional / only when trying to do test-driven development)
  2. run the tests to see it fail
devtools::test()

or CTRL+SHIFT+T (Win+Linux) or CMD+SHIFT+T (Mac)

  1. write (a draft of) a function in R/, document it with roxygen comments (R6 specifics here or by looking at R6 based packages)
  2. Load the function and "test" in the R terminal
devtools::load_all()

or CTRL+SHIFT+L (Win+Linux) or CMD+SHIFT+L (Mac)

  1. IF you already know how to do this, write unit test(s) for your function (see here) - if you don't know this yet, ask the others :)
  2. Run all tests to see whether everything is still working (see 2.)

Repeat 1-6

  1. Check your style and update your code accordingly
styler::style_pkg(style = styler::tidyverse_style, indent_by = 4) 

every once in a while / before committing

  1. check the package: this will not only run unit tests but will also check other aspects of the package, like whether documentation is complete, the DESCRIPTION file is ok, ...
devtools::check()

or

rcmdcheck::rcmdcheck()
  1. Update documentation: If you add or export new functions / classes or add / remove arguments, you should update the documentation
devtools::document()

or CTRL+SHIFT+D (Win+Linux) or CMD+SHIFT+D (Mac)

Shortcuts overview

Description Win + Linux Mac
Load All (devtools) CTRL+SHIFT+L CMD+SHIFT+L
Test Package (Desktop) CTRL+SHIFT+T CMD+SHIFT+T
Document Package CMD+SHIFT+D CMD+SHIFT+D
Check Package CMD+SHIFT+E CMD+SHIFT+E
(Build and Reload) CMD+SHIFT+B CMD+SHIFT+B

Note on VSCode: If you develop in VSCode, you can add shortcuts by opening your keybindings.json ("Open Keyboard Shortcuts (JSON)") and adding blocks like this:

{
    "description": "run all tests",
    "key": "cmd+shift+t",
    "command": "r.runCommand",
    "when": "editorLangId == r && editorTextFocus || terminalFocus",
    "args": "devtools::test()"
}

Misc

folders

  • R code goes into R
  • tests go into tests/testthat
  • man contains the documentation for the package (see below)
  • if we want to have internal or external data, this blog post is a great resource

dependencies

  • the NAMESPACE chapter is quite informative to read
  • add it to the "Imports" in the DESCRIPTION file -> this ensures that the package is installed when the user installs the package
  • in your function, use the :: notation to refer to the function, e.g. crul::HttpClient
  • according to R packages this is the preferred way
    • unless we use a function a lot, then we could also use the @importFrom roxygen tag, cf. here