
Documentation

Figure adapted from Packages, by Hannah Frick
BLT โโโ BLT.Rproj โโโ DESCRIPTION โโโ NAMESPACE โโโ R โ โโโ AR1.R โ โโโ BLT-package.R โ โโโ BLT_ggplot_na_distribution.R โ โโโ data.R โ โโโ perc_missing.R โ โโโ perc_missing_tidy.R โโโ README.Rmd โโโ README.md โโโ data โโโ data โ โโโ bacon.rda โ โโโ lettuce.rda โ โโโ tomatoes.rda โโโ man โ โโโ AR1.Rd โ โโโ BLT-package.Rd โ โโโ BLT_ggplot_na_distribution.Rd โ โโโ bacon.Rd โ โโโ figures โ โ โโโ README-pressure-1.png โ โโโ perc_missing.Rd โ โโโ perc_missing_tidy.Rd โโโ tests โ โโโ testthat โ โโโ testthat โ โ โโโ _snaps โ โ โ โโโ BLT_ggplot_na_distribution โ โ โ โโโ tomato-na-dist.new.svg โ โ โ โโโ tomato-na-dist.svg โ โ โโโ test-BLT_ggplot_na_distribution.R โ โ โโโ test-perc_missing_tidy.R โ โโโ testthat.R โโโ vignettes โโโ BLT.Rmd โโโ BLT.html
unchanged
changed
changed by you

Figure adapted from Packages, by Hannah Frick
BLT โโโ BLT.Rproj โโโ DESCRIPTION โโโ NAMESPACE โโโ R โ โโโ AR1.R โ โโโ BLT-package.R โ โโโ data.R โ โโโ perc_missing.R โ โโโ perc_missing_tidy.R โโโ README.Rmd โโโ README.md โโโ data โ โโโ bacon.rda โ โโโ lettuce.rda โ โโโ tomatoes.rda โโโ man โ โโโ AR1.Rd โ โโโ BLT-package.Rd โ โโโ bacon.Rd โ โโโ figures โ โ โโโ README-pressure-1.png โ โโโ perc_missing.Rd โ โโโ perc_missing_tidy.Rd โโโ vignettes โโโ BLT.Rmd โโโ BLT.html
unchanged
changed
changed by you
Metadata: The DESCRIPTION file โ โwhatโs in this package?โ
Object documentation: for functions and data
Package-level documentation
DESCRIPTIONDESCRIPTIONTitle: One line, title case, with no period. Fewer than 65 characters.
Version
for release: MAJOR.MINOR.PATCH version.
for development version: MAJOR.MINOR.PATCH.9000
Authors@R:
โautโ means author, โcreโ means creator, โctbโ means contributor.
DESCRIPTION (your turn)Open DESCRIPTION file to edit it:
Authors@R field uses options("usethis.description")
.Rprofile, so go back to the intro slides if you missed this!โ๏ธ edit Title field. Should be one line, title case, with no period. Fewer than 65 characters.
๐พ save DESCRIPTION file
BLT
โโโ BLT.Rproj
โโโ DESCRIPTION
โโโ NAMESPACE
โโโ R
โ โโโ AR1.R
โ โโโ BLT-package.R
โ โโโ data.R
โ โโโ perc_missing.R
โ โโโ perc_missing_tidy.R
โโโ data
โ โโโ bacon.rda
โ โโโ lettuce.rda
โ โโโ tomatoes.rda
unchanged
changed
changed by you
DESCRIPTIONPackage: BLT
Title: Work with Data on Bacon, Lettuce, and Tomatoes
Version: 0.0.0.9000
Authors@R:
person("Amelia", "McNamara", , "amelia.mcnamara@stthomas.edu", role = c("aut", "cre"))
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
license
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.3DESCRIPTIONDescription: One paragraph describing what the package does. Keep the width of the paragraph to 80 characters; indent subsequent lines with 4 spaces.
License
Encoding: How to encode text, use UTF-8 encoding.
LazyData: Use true to lazy-load data sets in the package.
DESCRIPTION๐ฌ Edit the description
๐ The full stop matters!
lets the world know how it can use the stuff you create
other choices include
use_apl2_license(): Apache 2.0use_gpl3_license(): GPL3uses option("usethis.full_name") or option("devtools.name")
.RprofileBLT
โโโ BLT.Rproj
โโโ DESCRIPTION
โโโ NAMESPACE
โโโ R
โ โโโ AR1.R
โ โโโ BLT-package.R
โ โโโ data.R
โ โโโ perc_missing.R
โ โโโ perc_missing_tidy.R
โโโ data
โ โโโ bacon.rda
โ โโโ lettuce.rda
โ โโโ tomatoes.rda
unchanged
changed
changed by you
Now would be a good time to commit your changes.
Jason Long, CC BY 3.0 <https://creativecommons.org/licenses/by/3.0>, via Wikimedia Commons
? or help()Files are written in a special โR documentationโ format: .Rd
.Rd resembles LaTeX
Hereโs parse_number.Rd from the readr package
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/collectors.R
\name{parse_number}
\alias{parse_number}
\alias{col_number}
\title{Parse numbers, flexibly}
\usage{
parse_number(x, na = c("", "NA"), locale = default_locale(), trim_ws = TRUE)
col_number()
}
\arguments{
\item{x}{Character vector of values to parse.}
\item{na}{Character vector of strings to interpret as missing values. Set this
option to \code{character()} to indicate no missing values.}
\item{locale}{The locale controls defaults that vary from place to place.
The default locale is US-centric (like R), but you can use
\code{\link[=locale]{locale()}} to create your own locale that controls things like
the default time zone, encoding, decimal mark, big mark, and day/month
names.}
๐ฅณ We donโt have to write it!!
We write โroxygen commentsโ in the .R files
These get turned into .Rd format
Functions from roxygen2 do the work but we can use devtools functions to call them.
makes it much easier to create and maintain your function documentation
lets you use a markdown-like syntax
BLT
โโโ BLT.Rproj
โโโ DESCRIPTION
โโโ NAMESPACE
โโโ R
โ โโโ AR1.R
โ โโโ BLT-package.R
โ โโโ data.R
โ โโโ perc_missing.R
โ โโโ perc_missing_tidy.R
โโโ data
โ โโโ bacon.rda
โ โโโ lettuce.rda
โ โโโ tomatoes.rda
unchanged
changed
changed by you
.R files#' indicates it is a roxygen commentdevtools::document() to convert roxygen comments to .Rd files.devtools::load_all()?#' indicates it is a roxygen comment
@ indicates a roxygen tag
@param arg โ describe the inputs@examplesโ show how the function works@seealso โ point out related functions@return โ describe the outputs@export โ is this a user visible functionOpen AR1.R (use_r() will do it if you donโt want to click)
Place your cursor on a blank line between the { โ๏ธ }
RStudio IDE menu: Code > Insert Roxygen Skeleton
๐ฌ Give your function a title and a brief description
๐ฌ Define the parameter(s)
๐ฌ Describe what the function returns
๐ฌ Save AR1.R
๐ฌ Run devtools::document() to turn roxygen comments to .Rd
โน Updating BLT documentation
โน Loading BLT
Writing NAMESPACE
Writing AR1.Rd
Warning message:
[AR1.R:13] @examples requires a value We will that fix later!
BLT โโโ BLT.Rproj โโโ DESCRIPTION โโโ NAMESPACE โโโ R โ โโโ AR1.R โ โโโ BLT-package.R โ โโโ perc_missing.R โ โโโ perc_missing_tidy.R โโโ data โ โโโ bacon.rda โ โโโ lettuce.rda โ โโโ tomatoes.rda โโโ man โ โโโ AR1.Rd
unchanged
changed
changed by you
NAMESPACE now lists the exported function
man/AR1.Rd has been written
๐ฌ Load the package : devtools::load_all()
๐ฌ Preview the documentation with:
๐ฎ ๐ฅณ
We need to add a usage example!
๐ฌ Under @examples, add one example for using your function
๐ฌ Save AR1.R, run devtools::document() followed by devtools::load_all()
๐ฌ Preview the documentation with ?AR1 and edit if needed
So far, weโve just written plain text in our documentation. But if you have spent time exploring R help pages, you will remember that there are often links. We can add those to our documentation, as well.
Take a few minutes and document your other functions. Recall:
RStudio IDE menu: Code > Insert Roxygen Skeleton
document()
load_all()
check()
Bonus: add examples for each
Now would be a good time to commit your changes
Jason Long, CC BY 3.0 <https://creativecommons.org/licenses/by/3.0>, via Wikimedia Commons
๐ฌ What happens if you do:
We can fix this with use_package_doc()
๐ฌ Add a package-level help page:
๐ฌ Run devtools::document() then devtools::load_all() followed by ?BLT
BLT โโโ BLT.Rproj โโโ DESCRIPTION โโโ NAMESPACE โโโ R โ โโโ AR1.R โ โโโ BLT-package.R โ โโโ perc_missing.R โ โโโ perc_missing_tidy. โโโ data โ โโโ bacon.rda โ โโโ lettuce.rda โ โโโ tomatoes.rda โโโ man โ โโโ AR1.Rd โ โโโ BLT-package.Rd
unchanged
changed
changed by you
devtools::check()This is a good time to run R CMD check to ensure our package is in full working order.
๐ฌ Use devtools to run R CMD check
Now would be a good time to commit your changes
Jason Long, CC BY 3.0 <https://creativecommons.org/licenses/by/3.0>, via Wikimedia Commons
Data gets documented in the data.R file. How can we create and open this file for editing?
Unfortunately, the trick to insert Roxygen skeleton doesnโt work with data, so we have to do the documentation more manually.
Hereโs what the who documentation in tidyr looks like
#' World Health Organization TB data
#'
#' A subset of data from the World Health Organization Global Tuberculosis
#' Report ...
#'
#' @format ## `who`
#' A data frame with 7,240 rows and 60 columns:
#' \describe{
#' \item{country}{Country name}
#' \item{iso2, iso3}{2 & 3 letter ISO country codes}
#' \item{year}{Year}
#' ...
#' }
#' @source <https://www.who.int/teams/global-tuberculosis-programme/data>
"who"
Can you write some documentation for the bacon data?
#' FRED bacon price data
#'
#' A subset of data on the average price of sliced bacon in the US.
#' Data is from 2016 to 2026
#'
#' @format ## `bacon`
#' A data frame with 119 rows and 5 columns:
#' \describe{
#' \item{date}{Date of observation}
#' \item{series_id}{APU0000704111, the series ID for the series}
#' \item{value}{the average price of bacon at that date}
#' \item{real_timestart, realtime_end}{the date the data was downloaded}
#' }
#' @source <https://fred.stlouisfed.org/series/APU0000704111>
"bacon"
BLT โโโ BLT.Rproj โโโ DESCRIPTION โโโ NAMESPACE โโโ R โ โโโ AR1.R โ โโโ BLT-package.R โ โโโ data.R โ โโโ perc_missing.R โ โโโ perc_missing_tidy.R โโโ data โ โโโ bacon.rda โ โโโ lettuce.rda โ โโโ tomatoes.rda โโโ man โ โโโ AR1.Rd โ โโโ BLT-package.Rd โ โโโ bacon.Rd
unchanged
changed
changed by you
document()
load_all()
check()
Now would be a good time to commit your changes
Jason Long, CC BY 3.0 <https://creativecommons.org/licenses/by/3.0>, via Wikimedia Commons
So far, we have just used use_package() to add Imports to our package. But, there are multiple levels of dependency possible.
Imports: must be installed for your package to work. If theyโre not, they will get installed.
Suggests: used by your package, but not required. Might provide data for examples, to run tests, build vignettes.
Depends: Avoid where possible. When your package requires a specific version of R, e.g. Depends: R (>= 3.4.0). Think critically: downstream effects on packages that depend on your package.
We used imputeTS::statsNA() multiple times in our package.
which will:
DESCRIPTION imports@importFrom imputeTS statsNA to BLT-package.R and NAMESPACE::@importFrom pkg fun๐ฌ Import statsNA() from imputeTS:
Now, we could go back and remove the imputeTS:: piece of our code.
Are used by your package, but not required for it to work
Might provide data for examples
Packages listed in Suggests are not automatically installed along with your package.
This means that you canโt assume the package is available
Now would be a good time to commit your changes
Jason Long, CC BY 3.0 <https://creativecommons.org/licenses/by/3.0>, via Wikimedia Commons
README.md: Really useful when sharing things on GitHub or GitLab. Can be the first file read by the user.
Vignette: A long-form guide to your package. Can describe the problem that your package is designed to solve, and then show the reader how to solve it.
Package website
README.mdBig picture goes in the README:
README.rmdYou are likely to have code in the README.md
Generating it with RMarkdown or Quarto is helpful
usethis helps out again!
usethis::use_readme_rmd() will:
README.Rmd which
README.Rmd to the .Rbuildignoremd before committingIf you would prefer to work in Quarto, you can! But youโll have to make the file yourself.
README.Rmd๐ฌ Add a README.Rmd with:
๐ฌ Examine your README.Rmd
README.Rmd rather than README.mdYou need to render README.Rmd regularly, to keep README.md up-to-date.
devtools::build_readme() is handy for this.
devtools::build_readme()README.mdman/figures/README-pressure-1.pngREADME.Rmd๐ฌ Complete: โThe goal of BLT is to โฆโ
๐ฌ Add: an example with AR1()
๐ฌ Remove: non-relevant sections
๐ฌ devtools::build_readme()
OR if it doesnโt work to install a temporary version of your package,
install() and then knit manually.
BLT โโโ BLT.Rproj โโโ DESCRIPTION โโโ NAMESPACE โโโ R โ โโโ AR1.R โ โโโ BLT-package.R โ โโโ data.R โ โโโ perc_missing.R โ โโโ perc_missing_tidy.R โโโ README.Rmd โโโ README.md โโโ data โ โโโ bacon.rda โ โโโ lettuce.rda โ โโโ tomatoes.rda โโโ man โ โโโ AR1.Rd โ โโโ BLT-package.Rd โ โโโ bacon.Rd โ โโโ figures โ โ โโโ README-pressure-1.png
unchanged
changed
changed by you
Complete: โThe goal of BLT is to โฆโ
The goal of BLT is to provide helper functions for time
series analysis, to work on data about bacon, lettuce, and
tomatoes. Remove: non-relevant sections
Everything after โWhat is special about usingโฆโฆโ
Now would be a good time to commit your changes.
Jason Long, CC BY 3.0 <https://creativecommons.org/licenses/by/3.0>, via Wikimedia Commons
README.Rmd (weโre skipping this)Badges give information to the user about the state of the package
To add this badge, we set up a โGitHub Actionโ
A GitHub Action is โcontinuous integrationโ which allows you to automate package building, testing and deployment on github
Actions we add are triggered when we push to GitHub.
R CMD check action (weโre skipping this)Adding the R CMD check action along with an edit to the README.Rmd will:
run R CMD check on multiple OS on GitHub and
add badge with the result to the README.Rmd
We can add with usethis::use_github_action_check_standard()
R-CMD-check.yaml (weโre skipping this)# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
name: R-CMD-check
jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}
name: ${{ matrix.config.os }} (${{ matrix.config.r }})
strategy:
fail-fast: false
matrix:
config:
- {os: macOS-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes
steps:
- uses: actions/checkout@v2
- uses: r-lib/actions/setup-pandoc@v2
- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check
- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
๐ฌ Add a vignette called โBLTโ with
โ Setting active project to "/Users/amcnamara2/BLT".
โ Adding knitr to Suggests field in DESCRIPTION.
โ Adding "inst/doc" to .gitignore.
โ Adding rmarkdown to Suggests field in DESCRIPTION.
โ Adding "knitr" to VignetteBuilder.
โ Creating vignettes/.
โ Adding "*.html" and "*.R" to vignettes/.gitignore.
โ Writing vignettes/BLT.Rmd.
โ Modify vignettes/BLT.Rmd.vignettes/BLT.Rmd๐ฌ Add info to vignettes/BLT.Rmd
The goal of BLT is to provide some time series helper functions, which allow you to work with data on bacon, lettuce, and tomatoes.
The package contains three sample datasets from FRED
The function AR1 is a convenience function to make a first-order autoregressive model. We can use this to model the bacon data
The function perc_missing allows you to compute the percent of a time series that is missing
Or, you can use the tidy version, perc_missing_tidy()
๐ฌ Build the vignette with:
โน Installing BLT in temporary library
โน Building /Users/amcnamara2/BLT/vignettes/BLT.Rmd
[WARNING] Deprecated: --highlight-style. Use --syntax-highlighting instead.(Iโm choosing to ignore this warningโ it sounds like something for the devtools developers, not me.)
BLT โโโ BLT.Rproj โโโ DESCRIPTION โโโ NAMESPACE โโโ R โ โโโ AR1.R โ โโโ BLT-package.R โ โโโ data.R โ โโโ perc_missing.R โ โโโ perc_missing_tidy.R โโโ README.Rmd โโโ README.md โโโ data โ โโโ bacon.rda โ โโโ lettuce.rda โ โโโ tomatoes.rda โโโ man โ โโโ AR1.Rd โ โโโ BLT-package.Rd โ โโโ bacon.Rd โ โโโ figures โ โ โโโ README-pressure-1.png โโโ vignettes โโโ BLT.Rmd โโโ BLT.html
unchanged
changed
changed by you
Now would be a good time to commit your changes.
Jason Long, CC BY 3.0 <https://creativecommons.org/licenses/by/3.0>, via Wikimedia Commons
pkgdown sites (weโre skipping this)pkgdown sites (weโre skipping this)pkgdown is designed to make it quick and easy to build a website for your package.
Examples
Very widely used
Uses your existing documentation
README.mdman/Now would be a good time to commit your changes.
Jason Long, CC BY 3.0 <https://creativecommons.org/licenses/by/3.0>, via Wikimedia Commons

Figure adapted from Packages, by Hannah Frick
BLT โโโ BLT.Rproj โโโ DESCRIPTION โโโ NAMESPACE โโโ R โ โโโ AR1.R โ โโโ BLT-package.R โ โโโ data.R โ โโโ perc_missing.R โ โโโ perc_missing_tidy.R โโโ README.Rmd โโโ README.md โโโ data โ โโโ bacon.rda โ โโโ lettuce.rda โ โโโ tomatoes.rda โโโ man โ โโโ AR1.Rd โ โโโ BLT-package.Rd โ โโโ bacon.Rd โ โโโ figures โ โ โโโ README-pressure-1.png โ โโโ perc_missing.Rd โ โโโ perc_missing_tidy.Rd โโโ vignettes โโโ BLT.Rmd โโโ BLT.html
unchanged
changed
changed by you
pkgdown sites (optional)DESCRIPTION
devtools workflow functionsdevtools::document()
#'@use_package()pkg::function()library()README.md with the purpose of the package, installation instructions and a simple exampleusethis::use_readme_rmd() will generate a template README.Rmd and do other useful thingsdevtools::build_readme() will build README.md from README.Rmdusethis::use_github_action_check_standard() will add the R CMD check action and a badgeusethis::use_vignette(name_of_vignette)pkgdown makes it quick and easy to build a website for your package from the existing documentationusethis::use_pkgdown() configures your package to use a pkgdown websitepkgdown::build_site() will build the website so you can examine it locallyusethis::use_pkgdown_github_pages() sets up a GitHub action to automatically build and publish your site on pushing.usethis really rocks!