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:
brew tap r-lib/rig
brew install --cask rigYou can use Scoop on Windows:
scoop bucket add r-bucket https://github.com/cderv/r-bucket.git
scoop install rigAfter installing rig, you can install the latest version of R using rig:
rig install releaseTo install the latest development version of R, use:
rig install develYou can switch between installed R versions using:
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():
pak::pak("pak")Then, you can install packages using pak::pak():
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():
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.RprojAdd core development infrastructure
From within your new package project, use usethis to set up common components:
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 changelogDevelopment workflow with devtools
devtools provides the core development workflow commands:
install.packages("devtools")| Command | Shortcut | Description |
|---|---|---|
devtools::load_all() | Ctrl/Cmd + Shift + L | Load all package code for interactive testing |
devtools::document() | Ctrl/Cmd + Shift + D | Generate documentation from roxygen2 comments |
devtools::test() | Ctrl/Cmd + Shift + T | Run all tests |
devtools::check() | Ctrl/Cmd + Shift + E | Run R CMD check (the full package validation suite) |
devtools::install() | Install the package locally |
Add dependencies
Use usethis to add package dependencies to DESCRIPTION:
usethis::use_package("dplyr") # Add to Imports
usethis::use_package("ggplot2", "Suggests") # Add to SuggestsSet up CI with GitHub Actions
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