install.packages(c("devtools", "roxygen2",
"usethis", "pkgdown"))R package development and pkgdown – Part 01
Pre-lecture activities
In advance of class, please install two additional packages:
devtools- this provides many additional tools for building packagesroxygen2- this provides tools for writing documentationpkgdown- this helps you to build a package website with little effortusethis- an automation package that simplifies project creation and setup
You can do this by calling
In addition, please read through:
Lecture
Acknowledgements
Material for this lecture was borrowed and adopted from
Learning objectives
At the end of this lesson you will:
- Create an empty R package
- Design a R function and write documentation
- Describe what a
DESCRIPTIONfile is and what goes in it - Be able to build and install a R package
- Build a website for your package
Slides
Class activity
For the rest of the time in class, you and your team will work on the final project. Stephanie will walk around to answer questions and happy to help in any way!
Post-lecture
Additional practice
Here are some additional practice questions to help you think about the material discussed.
Part 1
Here, our goal is to build a minimal R package. We will practice setting up a new package using usethis, writing functions, and documenting the functions using roxygen2.
- Create a package
- Use
usethis::create_package("simpleutils") - Look at the files created:
DESCRIPTION,NAMESPACE,R/, etc
- Add two functions
- function 01:
rescale01(x)= rescales a numeric vector to the range 0–1 - function 02:
count_missing(x)= returns the number of missing values
- Document each function using
roxygen2
- Add title, description,
@param,@return, and an example - Then run
devtools::document()to automatically create the help files (.Rd) and the NAMESPACE
- Install and test manually
- Install with
devtools::install().
- Load it and try running the some example code e.g.
library(simpleutils)
rescale01(c(1,5,10))
count_missing(c(1,NA,2,NA))Part 2
Next, we will add a small dataset to the package and write documentation for the dataset.
- Make a small tibble or data frame (e.g., 30 rows, with 2 numeric vars and 1 factor). Save it to the package using:
usethis::use_data(secret_data)- Create an R file under
R/with a roxygen block like:
#' Example dataset: secret_data
#'
#' A small example dataset for demonstration.
#'
#' @format A data frame with 30 rows and 3 variables:
#' \describe{
#' \item{x}{numeric}
#' \item{y}{numeric}
#' \item{group}{factor}
#' }
#' @source Simulated
"secret_data"- Run
devtools::document()thendevtools::install()to document and install the package.
library(simpleutils)
mydataPart 3
Finally, we will add a small vignette for our package.
Create a short vignette using
usethis::use_vignette("getting-started")Add text to it describing
- what the package is for?
- how to use
rescale01()? - how to use your dataset?
- Build and view the vignette using
devtools::build_vignettes(). You can view it usingbrowseVignettes("simpleutils")too.