Skip to contents

Overview

rdev supports my personal workflow, including creation of both traditional R packages and R Analysis Packages (vignette("analysis-package-layout")), enforcing consistency across packages, and providing Continuous Integration/Continuous Delivery (CI/CD) automation. I use the tools in rdev to improve code quality, speed up development of R code, and publish results of analyses in R and Quarto Notebooks as HTML to make them accessible to non-R users.

Installation

My current R development environment uses Homebrew, rig, RStudio, GitHub, a collection of R packages including rdev, Visual Studio Code, and Vim.

Homebrew

I use Homebrew Bundle to install all software on my systems; my basic macOS working environment is published on GitHub in macos-env.

Installing R

rig supports installation of multiple versions of official R binaries, which I use for reproducibility. To install R using rig, first install rig:

brew tap r-lib/rig
brew install --cask rig

Then install desired versions of R. The following installs R 3.6 through 4.4 on ARM based macs (as of 2024-09-03):

rig install oldrel/5 --without-pak --arch x86_64
rig install oldrel/4 --without-pak --arch x86_64
rig install oldrel/3 --without-pak
rig install oldrel/2 --without-pak
rig install oldrel/1 --without-pak
rig install oldrel --without-pak
rig install release --without-pak
rig default release

Note that:

  1. I don’t use pak, which rig installs by default, as it is not yet fully supported by renv.
  2. The oldest version of R I install is 3.6, since RStudio now requires R 3.6 or newer (as of version 2024.04.00)
  3. Since there was no ARM binary release for R 3.6 (oldrel/5) or R 4.0 (oldrel/4), I install the Intel binaries and run them with Rosetta 2.

Development Tools

Well, obviously, I use RStudio. RStudio is the leading IDE for R development and integrates with many R packages, although it sometimes falls short; I use GitHub and the command line for Git, and occasionally Visual Studio Code (which has better support for markdown) and Vim (which is faster for some types of edits).

Posit is developing a next-generation IDE based on Visual Studio Code, Positron. While I’ve tried using Positron, I’ve found that it’s still too new to replace RStudio for my development.

RStudio, the GitHub desktop client, and Visual Studio Code are easily installed using Homebrew:

brew install --cask rstudio
brew install --cask github
brew install --cask visual-studio-code

It is recommended to change the default settings for .RData in RStudio (in Options > General > Basic > Workspace):

  • Uncheck “Restore .Data into workspace at startup”
  • Set “Save workspace to .Data on exit” to “Never”

Vim is installed by default on macOS and most Unix-like systems.

Packages

Managing packages and environments are a challenge for most modern languages. Thankfully R doesn’t have the same level of challenge as python, or even ruby, managing packages available within a project is a best practice. I use renv for this purpose, and use renv to install and manage all packages in all of my projects.

The setup-r script from rdev installs a base set of packages needed to run rdev in the R User Library. A streamlined version of that script is included below.

# fix rig permissions
sudo chown -R "$(whoami)":admin /Library/Frameworks/R.framework/Versions/*/Resources/library

RVERSION="$(Rscript -e 'cat(as.character(getRversion()[1,1:2]))')"
USERLIB="$HOME/Library/R/$(uname -m)/${RVERSION}/library"
DEVPKG='c("renv", "styler", "lintr", "miniUI", "languageserver", "rmarkdown", "devtools", "available")'
GITPKG='c("jabenninghoff/rdev")'

if [ ! -d "${USERLIB}" ]
then
    mkdir -p "${USERLIB}"
fi

echo "install.packages(${DEVPKG}, repos=\"https://cloud.r-project.org\", lib=\"${USERLIB}\")" | R --no-save
echo "remotes::install_github(${GITPKG}, lib=\"${USERLIB}\")" | R --no-save

The chown command is needed to allow updating the base packages using the RStudio Packages “Update” function. I generally update packages in RStudio with no projects open before starting development, then update packages in projects using renv::update().

If you’re installing (development) versions of packages from GitHub, it is recommended to set up a personal access token using usethis::create_github_token() and adding it to your .Renviron as GITHUB_PAT= using usethis::edit_r_environ().

Further Reading

My workflow has been heavily influenced by the DevOps movement and the research of the DevOps Research and Assessment (DORA) team at Google started by Dr. Nicole Forsgren. Their research shows how technical and non-technical capabilities improve outcomes.

The functions included in rdev support many of the technical capabilities, including:

  • Code maintainability
  • Continuous delivery
  • Continuous integration
  • Deployment automation
  • Shifting left on security
  • Test automation
  • Trunk-based development
  • Version control

An outline of my SIRAcon 2022 talk, “Making R Work for You (With Automation)” is available on GitHub in siracon2022.