LCM Logo
Project Scaffolding

R

Installing R

Install R using rig

rig allows you to install and manage multiple R versions.
You can install rig using Homebrew on macOS or Linux:

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

You can use Scoop on Windows:

shell
scoop bucket add r-bucket https://github.com/cderv/r-bucket.git
scoop install rig

After installing rig, you can install the latest version of R using rig:

shell
rig install release

To install the latest development version of R, use:

shell
rig install devel

You can switch between installed R versions using:

shell
rig default <version>

Installing R packages

Install packages using pak

The pak package provides a fast and reliable way to install R packages and their dependencies. To use pak, first install it using install.packages():

R
pak::pak("pak")

Then, you can install packages using pak::pak():

R
pak::pak("package_name")

Project environments with renv (for advanced uses)

In R, you usually don't have to install dependencies for each project separately. Version conflicts are rare and maintaining a global package library is the default and recommended way of working with R. This avoids unnecessary duplication of packages on your disk.

If you do want or need to create a new R project, you can create a project-specific environment using renv.

Initialize a new R package

The recommended way to create a new R package is with usethis::create_package():

R
pak::pak("usethis")
usethis::create_package("path/to/packagename", rstudio = FALSE)

This creates a minimal package structure with the essential files:

packagename/
├── DESCRIPTION
├── NAMESPACE
├── R/
├── .Rbuildignore
└── packagename.Rproj

Add core development infrastructure

From within your new package project, use usethis to set up common components:

R
usethis::use_git()           # Initialize git repo
usethis::use_mit_license()   # Add a license (or use_gpl3_license(), etc.)
usethis::use_testthat()      # Set up unit testing with testthat
usethis::use_readme_rmd()    # Add a README.Rmd
usethis::use_package_doc()   # Add package-level documentation
usethis::use_news_md()       # Add a NEWS.md changelog

Development workflow with devtools

devtools provides the core development workflow commands:

R
install.packages("devtools")
CommandShortcutDescription
devtools::load_all()Ctrl/Cmd + Shift + LLoad all package code for interactive testing
devtools::document()Ctrl/Cmd + Shift + DGenerate documentation from roxygen2 comments
devtools::test()Ctrl/Cmd + Shift + TRun all tests
devtools::check()Ctrl/Cmd + Shift + ERun R CMD check (the full package validation suite)
devtools::install()Install the package locally

Add dependencies

Use usethis to add package dependencies to DESCRIPTION:

R
usethis::use_package("dplyr")              # Add to Imports
usethis::use_package("ggplot2", "Suggests") # Add to Suggests

Set up CI with GitHub Actions

R
usethis::use_github_action("check-standard") # R CMD check on multiple platforms
usethis::use_github_action("test-coverage")  # Code coverage reporting
usethis::use_github_action("pkgdown")        # Auto-build documentation site

Resources

Programming for Data Science in R (PDSR)

On this page