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
NAMESPACEfile - 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
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:
- Restart R (Session > Restart R)
- Load your package:
library(mypackage)- Try your function and data:
add_numbers(4, 5)
my_dataset- Try help pages:
?add_numbers
?my_datasetBonus: 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