R Development Skill
Sub-skills
| Sub-skill |
Description |
| r-dev-package |
devtools, usethis, roxygen2 |
| r-dev-testing |
testthat, covr, mockery |
| r-dev-docs |
pkgdown, vignettes, README |
| r-dev-oop |
R6, S3, S4 classes |
R package development and tooling.
Package Development
| Package |
Description |
| devtools ★ |
Package development tools |
| usethis ★ |
Workflow automation |
| roxygen2 ★ |
Documentation from comments |
| pkgdown |
Package websites |
| testthat ★ |
Unit testing |
| covr |
Test coverage |
Code Quality
| Package |
Description |
| lintr ★ |
Static code analysis |
| styler |
Code formatting |
| goodpractice |
Package best practices |
Documentation
| Package |
Description |
| roxygen2 ★ |
In-source documentation |
| sinew |
Generate roxygen skeletons |
| staticdocs |
Static HTML docs |
OOP Systems
| Package |
Description |
| R6 ★ |
Encapsulated OOP |
| S7 |
Next-gen OOP (experimental) |
Debugging & Profiling
| Package |
Description |
| pryr |
Understand R internals |
| lineprof |
Line profiling |
| profvis |
Interactive profiling |
Environment & Reproducibility
| Package |
Description |
| renv ★ |
Project environments |
| box |
Modern module system |
| import |
Import mechanism |
| installr |
Install software (Windows) |
Async & Promises
| Package |
Description |
| promises |
Async programming |
| future |
Parallel/distributed processing |
Deployment
| Package |
Description |
| Rocker |
Docker configurations |
| drat |
R repositories on GitHub |
IDEs & Editors
| Tool |
Description |
| RStudio ★ |
Full-featured IDE |
| VSCode + vscode-R |
VS Code support |
| radian |
Modern R console |
| IRkernel |
Jupyter kernel |
| Nvim-R |
Neovim plugin |
Quick Examples
# Create new package
usethis::create_package("mypackage")
# Add dependencies
usethis::use_package("dplyr")
usethis::use_pipe() # Add %>%
# Documentation
usethis::use_roxygen_md()
devtools::document()
# Testing
usethis::use_testthat()
usethis::use_test("myfunction")
devtools::test()
# Check package
devtools::check()
# Build and install
devtools::build()
devtools::install()
# roxygen2 documentation
#' Add two numbers
#'
#' @param x A number
#' @param y A number
#' @return The sum of x and y
#' @export
#' @examples
#' add(1, 2)
add <- function(x, y) {
x + y
}
# testthat tests
test_that("add works", {
expect_equal(add(1, 2), 3)
expect_equal(add(-1, 1), 0)
expect_error(add("a", 1))
})
# R6 class
library(R6)
Person <- R6Class("Person",
public = list(
name = NULL,
initialize = function(name) {
self$name <- name
},
greet = function() {
cat("Hello, I'm", self$name, "\n")
}
)
)
p <- Person$new("Alice")
p$greet()
# renv workflow
renv::init() # Initialize
renv::snapshot() # Save state
renv::restore() # Restore state
# Profiling
profvis::profvis({
# Code to profile
result <- slow_function(data)
})
Package Development Workflow
# 1. Create package
usethis::create_package("mypackage")
# 2. Set up Git
usethis::use_git()
usethis::use_github()
# 3. Add license
usethis::use_mit_license()
# 4. Set up testing
usethis::use_testthat()
# 5. Add CI
usethis::use_github_action_check_standard()
# 6. Write code in R/
# 7. Document with roxygen2
devtools::document()
# 8. Write tests in tests/testthat/
devtools::test()
# 9. Check package
devtools::check()
# 10. Build pkgdown site
usethis::use_pkgdown()
pkgdown::build_site()
Resources
1---2name: r-dev3description: R development packages. Use for package development, testing, documentation, code style, and IDE setup.4metadata:5 source: https://skillmd.com/skills/leolin990405/r-dev6---78# R Development Skill910## Sub-skills1112| Sub-skill | Description |13|-----------|-------------|14| [r-dev-package](r-dev-package/SKILL.md) | devtools, usethis, roxygen2 |15| [r-dev-testing](r-dev-testing/SKILL.md) | testthat, covr, mockery |16| [r-dev-docs](r-dev-docs/SKILL.md) | pkgdown, vignettes, README |17| [r-dev-oop](r-dev-oop/SKILL.md) | R6, S3, S4 classes |1819R package development and tooling.2021## Package Development2223| Package | Description |24|---------|-------------|25| **devtools** ★ | Package development tools |26| **usethis** ★ | Workflow automation |27| **roxygen2** ★ | Documentation from comments |28| **pkgdown** | Package websites |29| **testthat** ★ | Unit testing |30| **covr** | Test coverage |3132## Code Quality3334| Package | Description |35|---------|-------------|36| **lintr** ★ | Static code analysis |37| **styler** | Code formatting |38| **goodpractice** | Package best practices |3940## Documentation4142| Package | Description |43|---------|-------------|44| **roxygen2** ★ | In-source documentation |45| **sinew** | Generate roxygen skeletons |46| **staticdocs** | Static HTML docs |4748## OOP Systems4950| Package | Description |51|---------|-------------|52| **R6** ★ | Encapsulated OOP |53| **S7** | Next-gen OOP (experimental) |5455## Debugging & Profiling5657| Package | Description |58|---------|-------------|59| **pryr** | Understand R internals |60| **lineprof** | Line profiling |61| **profvis** | Interactive profiling |6263## Environment & Reproducibility6465| Package | Description |66|---------|-------------|67| **renv** ★ | Project environments |68| **box** | Modern module system |69| **import** | Import mechanism |70| **installr** | Install software (Windows) |7172## Async & Promises7374| Package | Description |75|---------|-------------|76| **promises** | Async programming |77| **future** | Parallel/distributed processing |7879## Deployment8081| Package | Description |82|---------|-------------|83| **Rocker** | Docker configurations |84| **drat** | R repositories on GitHub |8586## IDEs & Editors8788| Tool | Description |89|------|-------------|90| **RStudio** ★ | Full-featured IDE |91| **VSCode + vscode-R** | VS Code support |92| **radian** | Modern R console |93| **IRkernel** | Jupyter kernel |94| **Nvim-R** | Neovim plugin |9596## Quick Examples9798```r99# Create new package100usethis::create_package("mypackage")101102# Add dependencies103usethis::use_package("dplyr")104usethis::use_pipe() # Add %>%105106# Documentation107usethis::use_roxygen_md()108devtools::document()109110# Testing111usethis::use_testthat()112usethis::use_test("myfunction")113devtools::test()114115# Check package116devtools::check()117118# Build and install119devtools::build()120devtools::install()121122# roxygen2 documentation123#' Add two numbers124#'125#' @param x A number126#' @param y A number127#' @return The sum of x and y128#' @export129#' @examples130#' add(1, 2)131add <- function(x, y) {132 x + y133}134135# testthat tests136test_that("add works", {137 expect_equal(add(1, 2), 3)138 expect_equal(add(-1, 1), 0)139 expect_error(add("a", 1))140})141142# R6 class143library(R6)144Person <- R6Class("Person",145 public = list(146 name = NULL,147 initialize = function(name) {148 self$name <- name149 },150 greet = function() {151 cat("Hello, I'm", self$name, "\n")152 }153 )154)155p <- Person$new("Alice")156p$greet()157158# renv workflow159renv::init() # Initialize160renv::snapshot() # Save state161renv::restore() # Restore state162163# Profiling164profvis::profvis({165 # Code to profile166 result <- slow_function(data)167})168```169170## Package Development Workflow171172```r173# 1. Create package174usethis::create_package("mypackage")175176# 2. Set up Git177usethis::use_git()178usethis::use_github()179180# 3. Add license181usethis::use_mit_license()182183# 4. Set up testing184usethis::use_testthat()185186# 5. Add CI187usethis::use_github_action_check_standard()188189# 6. Write code in R/190# 7. Document with roxygen2191devtools::document()192193# 8. Write tests in tests/testthat/194devtools::test()195196# 9. Check package197devtools::check()198199# 10. Build pkgdown site200usethis::use_pkgdown()201pkgdown::build_site()202```203204## Resources205206- R Packages book: https://r-pkgs.org/207- devtools: https://devtools.r-lib.org/208- testthat: https://testthat.r-lib.org/209- roxygen2: https://roxygen2.r-lib.org/210- R6: https://r6.r-lib.org/