R Package Development Exercises: 20 Practice Problems

Twenty practice problems on R package development: usethis, devtools, roxygen2, testthat, DESCRIPTION, NAMESPACE, vignettes, CRAN.

library(dplyr)
library(pkgdown)
# library(usethis); library(devtools); library(roxygen2); library(testthat)

Exercise 1: Create a package skeleton

Difficulty: Beginner.

Show solution
# usethis::create_package("mypkg")

Exercise 2: Add a function in R/

Difficulty: Beginner.

Show solution
# usethis::use_r("hello")   # creates R/hello.R

Exercise 3: Document with roxygen

Difficulty: Intermediate.

#' Say hello
#' @param name character
#' @return character greeting
#' @export
hello <- function(name) paste("Hi", name)
Show solution

Run devtools::document() to convert roxygen comments to .Rd files and update NAMESPACE.

Exercise 4: Run tests

Difficulty: Intermediate.

Show solution
# devtools::test()

Exercise 5: Add a test

Difficulty: Intermediate.

Show solution
# usethis::use_test("hello")
# Inside tests/testthat/test-hello.R:
# test_that("greets correctly", { expect_equal(hello("R"), "Hi R") })

Exercise 6: Check package

Difficulty: Intermediate.

Show solution
# devtools::check()

Exercise 7: Build manual

Difficulty: Intermediate.

Show solution
# devtools::build_manual()

Exercise 8: Add license

Difficulty: Intermediate.

Show solution
# usethis::use_mit_license("Your Name")

Exercise 9: Add Imports

Difficulty: Intermediate.

Show solution
# usethis::use_package("dplyr")

Exercise 10: Add a Suggests dependency

Difficulty: Intermediate.

Show solution
# usethis::use_package("ggplot2", type = "Suggests")

Exercise 11: Add data

Difficulty: Intermediate.

Show solution
# mydata <- mtcars
# usethis::use_data(mydata)

Exercise 12: Add vignette

Difficulty: Advanced.

Show solution
# usethis::use_vignette("intro")

Exercise 13: README setup

Difficulty: Beginner.

Show solution
# usethis::use_readme_rmd()

Exercise 14: pkgdown website

Difficulty: Advanced.

Show solution
# usethis::use_pkgdown(); pkgdown::build_site()

Exercise 15: Continuous integration (GitHub Actions)

Difficulty: Advanced.

Show solution
# usethis::use_github_action_check_standard()

Exercise 16: Bump version

Difficulty: Beginner.

Show solution
# usethis::use_version("minor")

Exercise 17: Install local

Difficulty: Beginner.

Show solution
# devtools::install()

Exercise 18: Load without install

Difficulty: Beginner.

Show solution
# devtools::load_all()

Exercise 19: Add @importFrom

Difficulty: Advanced.

#' @importFrom dplyr filter
my_fn <- function(x) dplyr::filter(x, ...)
Show solution

Adds the import to NAMESPACE via roxygen so the namespaced function is available.

Exercise 20: Submit to CRAN (concept)

Difficulty: Advanced.

Show solution
# devtools::release()
# Follows checklist: check on multiple platforms, update cran-comments, etc.

What to do next

  • testthat-Exercises (coming), testing deep dive.
  • roxygen2-Exercises (coming), documentation.

Ready to earn the R Package Development Certificate?

The quiz is concept-based and respects your time: pass it once and your verifiable certificate is yours to share on LinkedIn, your resume, or your portfolio. Take it when you feel comfortable with the material.

Attempt the quiz