Michael Lydeamore
  • Home
  • CV
  • Software
  • Blog
  • Guides
    • Making your first R Package
  1. Build, Check, and Install
  • Intro to R Packages with {usethis}
  • Why Write an R Package?
  • Creating a New Package
  • Writing Your First Function
  • Working with Data
  • Build, Check, and Install
  • Final Tips & Resources

On this page

  • Step 1: Document Everything
  • Step 2: Run Package Checks
  • Step 3: Install Locally
  • Step 4: Try It Out
  • Optional: Share via GitHub
  • Bonus: Build a README
  • Common Gotchas
  • What’s Next?

Build, Check, and Install

Now that your package has:

  • A function
  • Some documentation
  • (Maybe) some data

… it’s time to build, check, and install it!

These are standard steps for developing R packages, and {devtools} makes them easy.


Step 1: Document Everything

Before building, make sure all documentation and the NAMESPACE are up to date:

devtools::document()

This will:

  • Generate help files (man/*.Rd)
  • Update the NAMESPACE file
  • Warn you if any roxygen is incomplete

Step 2: Run Package Checks

devtools::check()

This simulates what CRAN does before accepting a package.

It checks:

  • Missing documentation
  • Broken examples
  • Syntax errors
  • Suggested packages
  • Licensing and metadata
Tip

A package that passes check() is ready to share or install.


Step 3: Install Locally

Once check() is clean:

devtools::install()

This compiles your package and installs it into your system R library.

Then, you can use it like any other package:

library(mypackage)

Step 4: Try It Out

Check that everything works from a clean session:

  1. Restart R (Session > Restart R)
  2. Load your package:
library(mypackage)
  1. Try your function and data:
add_numbers(4, 5)
my_dataset
  1. Try help pages:
?add_numbers
?my_dataset

Optional: Share via GitHub

If your package is version-controlled with Git, and you have a GitHub account, you can publish it:

usethis::use_github()

This will:

  • Create a GitHub repo
  • Push your code
  • Set the remote for future commits

Then others can install it directly:

devtools::install_github("yourname/mypackage")

📢 No CRAN required.


Bonus: Build a README

Use this to generate a README.Rmd with example usage:

usethis::use_readme_rmd()

Then knit it. This gives users a friendly intro when they land on your GitHub repo.


Common Gotchas

Problem Fix
Function not found after install Did you run @export and document()?
Help page missing Roxygen missing or malformed
check() fails with notes Read the messages carefully — not all notes are fatal
Function works in dev, not after install Try restarting R and loading the installed version

What’s Next?

Congrats! You’ve built and installed a working R package.

Next steps:

  • Add tests with usethis::use_testthat()
  • Set up continuous integration (CI)
  • Consider submitting to CRAN (optional!)
  • Keep improving and versioning your work

👉 Final Tips & Resources

Dr. Michael Lydeamore
Lecturer in Business Analytics
Monash University
© Copyright 2023 Michael Lydeamore

 

@MikeLydeamore
MikeLydeamore
michael.lydeamore@monash.edu
Clayton, VIC, Australia