Build your own toolbox 🧰

R package development

2025-09-03

Set-up πŸ‘©β€πŸ’»

  • Welcome! We’ll start the workshop shortly

  • If you want to code along with me, please follow the set-up instructions ⬇️

statsrhian.github.io/build-r-pkg/set-up

  • If you’d prefer to just want to sit back and watch, that’s fine too 😊

  • WiFi: RSS edin2025

Welcome

What is an R package?

A toolbox with the lid locked in the closed position.

Why build one?

  • Reproducible
  • Reusability across projects
  • Testing is easier
  • Dependency management
  • Easy to use documentation
  • It’s fun and empowering
  • Sharing is caring

Myths

It doesn’t have to…

  • have hundreds of functions
  • be useful for anyone else
  • do anything fancy
  • be on CRAN
  • be scary

Your personal toolbox

It’s not just any box of random tools β€” it’s a kit that someone else should be able to pick up and use immediately without calling you for instructions.

What’s in your toolbox?

biketools/

A toolbox with the lid locked in the closed position.

DESCRIPTION

biketools/
β”œβ”€ DESCRIPTION

The lid of a toolbox with a label on it, describing that the box contains bike tools. The label also says who the toolbox belongs to and featues a version number.

R/ folder

biketools/
β”œβ”€ DESCRIPTION
β”œβ”€ R/
β”‚  β”œβ”€ chain-braker.R
β”‚  β”œβ”€ multitool.R
β”‚  β”œβ”€ pedal-wrench.R
β”‚  β”œβ”€ tyre-levers.R

The toolbox with the lid open to reveal a collection of 4 different bicycle maintenance tools sitting on a tray.

man/ folder

biketools/
β”œβ”€ DESCRIPTION
β”œβ”€ R/
β”‚  β”œβ”€ chain-braker.R
β”‚  β”œβ”€ multitool.R
β”‚  β”œβ”€ pedal-wrench.R
β”‚  β”œβ”€ tyre-levers.R
β”œβ”€ man/
β”‚  β”œβ”€ chain-braker.Rd
β”‚  β”œβ”€ multitool.Rd
β”‚  β”œβ”€ pedal-wrench.Rd
β”‚  β”œβ”€ tyre-levers.Rd

An open toolbox with a yellow label stuck to each bicycle maintenance tool that is on the tray. Each label gives the name of that tool and a description of how to use the tool.

NAMESPACE

biketools/
β”œβ”€ DESCRIPTION
β”œβ”€ R/
β”‚  β”œβ”€ chain-braker.R
β”‚  β”œβ”€ multitool.R
β”‚  β”œβ”€ pedal-wrench.R
β”‚  β”œβ”€ tyre-levers.R
β”œβ”€ man/
β”‚  β”œβ”€ chain-braker.Rd
β”‚  β”œβ”€ multitool.Rd
β”‚  β”œβ”€ pedal-wrench.Rd
β”‚  β”œβ”€ tyre-levers.Rd
β”œβ”€ NAMESPACE

The open toolbox shows a tray of 4 tools at the top, with a single label stating the names of the 4 tools you can use. The label also advises that you might need to borrow gloves from Sally and another wrench from Jack. The tray sits at an angle allowing a glimpse at a collection of tools hiding at the bottom of the toolbox.

Extra bits

biketools/
β”œβ”€ DESCRIPTION
β”œβ”€ R/
β”‚  β”œβ”€ chain-braker.R
β”‚  β”œβ”€ multitool.R
β”‚  β”œβ”€ pedal-wrench.R
β”‚  β”œβ”€ tyre-levers.R
β”œβ”€ man/
β”‚  β”œβ”€ chain-braker.Rd
β”‚  β”œβ”€ multitool.Rd
β”‚  β”œβ”€ pedal-wrench.Rd
β”‚  β”œβ”€ tyre-levers.Rd
β”œβ”€ NAMESPACE
β”œβ”€ data/
β”œβ”€ pkgdown/
β”œβ”€ tests/
β”œβ”€ vignettes/
β”œβ”€ README.md
β”œβ”€ NEWS.md

The closed toolbox has a label on the side stating the toolbox has been checked and providing a longer description of what to use the toolbox for. Behind the toolbox is a spare tyre and an illustrated book of bicycle maintenance.

Let’s build πŸ› οΈ

πŸ‘€ Create an R package

πŸ‘©β€πŸ’» Create an R package

  • Run usethis::create_package("sums")
  • Open the DESCRIPTION file and customise it
  • Run usethis::use_mit_license()
  • Run devtools::install()

πŸ‘€ Add a function

πŸ‘©β€πŸ’» Add a function

  • Copy the add() function in R/add.R

    add <- function(x, y) {
      x + y
    }
  • Add export(add) to NAMESPACE

  • Run devtools::load_all()

  • Try out your function add(3, 4)

πŸ‘€ Document your function

πŸ‘©β€πŸ’» Document your function

  1. Add some {roxygen2} strings

    #' Add together two numbers
    #' 
    #' @param x A number.
    #' @param y A number.
    #' @returns A numeric vector.
    #' @examples
    #' add(1, 1)
    #' add(10, 1)
    #' @export
    add <- function(x, y) {
      x + y
    }
  2. Run devtools::document(); devtools::load_all()

  3. View the help file ?add

Package check

Inspect your toolbox before lending it out

  • Do all the tools work?
  • Does they have instruction manuals?
  • Is there anything in there that shouldn’t be in there?
  • Is it too heavy?

πŸ‘€ Package check

πŸ‘©β€πŸ’» Package check

  1. Run devtools::check()

Don’t worry if it doesn’t all pass!

πŸ‘€ Using other functions

πŸ‘©β€πŸ’» Using other functions

  1. Add cli_alert_success("You added two numbers") to add()

  2. Run check

  3. Prefix with cli::

  4. Run check

  5. Use usethis::use_package("cli") to update Imports:

  6. Run check

Usethis

  • use_readme_md()

  • use_roxygen_md()

  • use_news_md()

  • use_github_actions()

  • use_data()

  • use_testthat()

  • use_vignettes()

  • use_pkgdown()

Developer process πŸ”„

Editing code

  1. Make code changes
  2. devtools::load_all()

Add new function

  1. Create and @export
  2. devtools::document()
  3. devtools::load_all()

Editing docs

  1. Edit function docs
  2. devtools::document()

All changes ready

  1. devtools::check()
  2. devtools::install()
  3. Publish code

Share your package

  • Local install: devtools::install_local("biketools")

  • GitHub: devtools::install_github("statsrhian/biketools")

  • CRAN: install.packages("biketools.zip")

Go forth and build! πŸ‘·β€β™€οΈ

  • Remember to make a hex sticker

  • Let me know how you get on πŸ“§ @statsRhian

Example hex sticker showing the RSSConf2025 hashtag and and image of Edinburgh Castle