?uss_make_matches
Day 1 Session 2: 🏷️Documentation - Minimal
Invalid Date
At the end of this section you will be able to:
DESCRIPTION
file and know what to edit manually and which parts devtools
will edit automagicallydevtools
workflow to convert to “R documentation” format .Rd
Metadata: The DESCRIPTION
file – “what’s in this package?”
Object documentation: for functions and data
Package-level documentation
pkgdown
(Wickham, Hesselberth, and Salmon 2022) sites: Websites for your package!DESCRIPTION
DESCRIPTION
🎬 Take a look at the DESCRIPTION
for the following packages. What’s common? What’s different?
ggplot2
(Wickham 2016)
palmerpenguins
(Horst, Hill, and Gorman 2020)
DESCRIPTION
Title: 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
🎬 Edit the title
🎬 You should find that your information as an author is already included because you edited your .RProfile
at the start of the session
DESCRIPTION
Package: ussie
Title: Work with European Football League Data
Version: 0.0.0.9000
Authors@R:
person("Emma", "Rand", , "emma.rand@york.ac.uk", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-1358-8275"))
Description: What the package does (one paragraph).
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.0
DESCRIPTION
Description: 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!
Now would be a good time to commit your changes and push them to GitHub.
Or just commit
?
or help()
🎬 Try this now:
?uss_make_matches
Files are written in a special “R documentation” format: .Rd
.Rd
resembles LaTeX,
man/uss_make_matches.Rd
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/matches.R
\name{uss_make_matches}
\alias{uss_make_matches}
\title{Make a standard league-play tibble}
\usage{
uss_make_matches(data_engsoc, country)
}
\arguments{
\item{data_engsoc}{obtained from {engsoccerdata}.}
\item{country}{\code{character} scalar, specifies the league.}
}
\value{
a tibble with columns \code{country}, \code{date}, \code{season}, \code{tier}, \code{home},
\code{visitor}, \code{goals_home}, \code{goals_visitor}.
}
\description{
Given a league-play data frame from {engsoccer}, returns a tibble with
standardised column-names and types, e.g. \code{date} is a \code{Date}.
}
\examples{
uss_make_matches(engsoccerdata::spain, "Spain")
}
🥳 We don’t have to write it!!
We write “roxygen comments” in the .R
files
These get turned into .Rd
format
roxygen2
(Wickham et al. 2022) do the work but we can use devtools
functions to call them#' Make a standard league-play tibble
#'
#' Given a league-play data frame from {engsoccer}, returns a tibble with
#' standardised column-names and types, e.g. `date` is a `Date`.
#'
#' @param data_engsoc obtained from {engsoccerdata}.
#' @param country `character` scalar, specifies the league.
#'
#' @return a tibble with columns `country`, `date`, `season`, `tier`, `home`,
#' `visitor`, `goals_home`, `goals_visitor`.
#' @export
#'
#' @examples
#' uss_make_matches(engsoccerdata::spain, "Spain")
uss_make_matches <- function(data_engsoc, country) {
result <-
data_engsoc |>
tibble::as_tibble() |>
dplyr::transmute(
country = as.character(country),
tier = as.integer(tier),
season = as.integer(Season),
date = as.Date(Date),
home = as.character(home),
visitor = as.character(visitor),
goals_home = as.integer(hgoal),
goals_visitor = as.integer(vgoal)
)
result
}
#'
indicates it is a roxygen comment.R
files.devtools::document()
to convert roxygen comments to .Rd
files.devtools::load_all()
?
🎬 Open matches.R
usethis::use_r("matches")
. . .
🎬 Put your cursor anywhere in the function and do Code | Insert Roxygen Documentation (Ctrl-Alt-Shift-R)
#' Title
#'
#' @param data_engsoc
#' @param country
#'
#' @return
#' @export
#'
#' @examples
uss_make_matches <- function(data_engsoc, country) {
result <-
data_engsoc |>
tibble::as_tibble() |>
dplyr::transmute(
country = as.character(country),
tier = as.integer(tier),
season = as.integer(Season),
date = as.Date(Date),
home = as.character(home),
visitor = as.character(visitor),
goals_home = as.integer(hgoal),
goals_visitor = as.integer(vgoal)
)
result
}
#'
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 function🎬 Give your function a title and a brief description
🎬 Define the two parameters
🎬 Describe what the function returns
#' Make a standard league-play tibble
#'
#' Given a league-play data frame from {engsoccer}, returns a tibble with
#' standardised column-names and types, e.g. `date` is a `Date`.
#'
#' @param data_engsoc obtained from {engsoccerdata}.
#' @param country `character` scalar, specifies the league.
#'
#' @return a tibble with columns `country`, `date`, `season`, `tier`, `home`,
#' `visitor`, `goals_home`, `goals_visitor`.
#' @export
#'
#' @examples
🎬 Save matches.R
🎬 Run devtools::document()
to turn roxygen comments to .Rd
devtools::document()
We will that fix later!
├── DESCRIPTION
├── LICENSE
├── LICENSE.md
├── man
│ └── uss_make_matches.Rd
├── NAMESPACE
├── R
│ └── matches.R
└── ussie.Rproj
NAMESPACE
now lists the exported function
man/uss_make_matches.Rd
has been written
🎬 Load the package : devtools::load_all()
🎬 Preview the documentation with:
?uss_make_matches
😮 🥳
We need to add a usage example!
🎬 Under @examples
, add one example for using your function
🎬 Save matches.R
, run devtools::document()
followed by devtools::load_all()
🎬 Preview the documentation with ?uss_make_matches
and edit if needed
🎬 What happens if you do:
?ussie
We can fix this with use_package_doc()
🎬 Add a package-level help page:
usethis::use_package_doc()
🎬 Run devtools::document()
then devtools::load_all()
followed by ?ussie
Now would be a good time to commit your changes and push them to GitHub
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
devtools::check()
── R CMD check results ─────────────────────────────────── ussie 0.0.0.9000 ────
Duration: 40.4s
❯ checking dependencies in R code ... WARNING
'::' or ':::' imports not declared from:
'dplyr' 'tibble'
❯ checking for unstated dependencies in examples ... WARNING
'::' or ':::' import not declared from: 'engsoccerdata'
❯ checking R code for possible problems ... NOTE
uss_make_matches: no visible binding for global variable 'tier'
uss_make_matches: no visible binding for global variable 'Season'
uss_make_matches: no visible binding for global variable 'Date'
uss_make_matches: no visible binding for global variable 'home'
uss_make_matches: no visible binding for global variable 'visitor'
uss_make_matches: no visible binding for global variable 'hgoal'
uss_make_matches: no visible binding for global variable 'vgoal'
Undefined global functions or variables:
Date Season hgoal home tier vgoal visitor
0 errors ✔ | 2 warnings ✖ | 1 note ✖
We’ve gained a warning!
Our warnings are about Package dependencies
Our warnings are because we have used packages that we have not declared officially.
We need to document our package dependencies
Our users, and the package installation machinery, need to know what our package depends on before installing
Levels of dependency
Depends: R (>= 3.4.0)
. Think critically: downstream effects on packages that depend on your package.usethis
again!
use_package(package, type = "Imports")
usethis::use_package()
🎬 Use usethis::use_package()
to add the dplyr
package to Imports
usethis::use_package("dplyr")
usethis::use_package()
Note: we get reminded to “Refer to functions with dplyr::fun()
”
That is, we do NOT use library(dplyr)
to make functions available to our package.
devtools::check()
🎬 Run devtools::check()
on your package again
── R CMD check results ─────────── ussie 0.0.0.9000 ────
Duration: 24s
❯ checking dependencies in R code ... WARNING
'::' or ':::' import not declared from: 'tibble'
❯ checking for unstated dependencies in examples ... WARNING
'::' or ':::' import not declared from: 'engsoccerdata'
❯ checking R code for possible problems ... NOTE
uss_make_matches: no visible binding for global variable 'tier'
uss_make_matches: no visible binding for global variable 'Season'
uss_make_matches: no visible binding for global variable 'Date'
uss_make_matches: no visible binding for global variable 'home'
uss_make_matches: no visible binding for global variable 'visitor'
uss_make_matches: no visible binding for global variable 'hgoal'
uss_make_matches: no visible binding for global variable 'vgoal'
Undefined global functions or variables:
Date Season hgoal home tier vgoal visitor
0 errors ✔ | 2 warnings ✖ | 1 note ✖
Three types, according to how much your are using the imported package.
If you are using….
Imports:
field of the DESCRIPTION
file and call the function(s) explicitly using ::
, e.g., pkg::fun()
.::
by importing the function with @importFrom pkg fun
in ussie-package.R
.@import package
. But beware, less transparent, higher likelihood of conflicting function names.We will use tibble::tible()
multiple times.
Instead of using:
usethis::use_package("tibble")
we can use
usethis::use_import_from("tibble", "tibble")
which will:
DESCRIPTION
imports@importFrom tibble tibble
to ussie-package.R
and NAMESPACE
::
@importFrom pkg fun
🎬 Import tibble()
from tibble
:
usethis::use_import_from("tibble", "tibble")
Are used by your package, but not required for it to work
Might provide data for examples
In our case:
matches.R
#' @examples
#' uss_make_matches(engsoccerdata::spain, "Spain")
🎬 Add engsoccerdata
usethis::use_package("engsoccerdata",
type = "Suggests")
Packages listed in Suggests are not automatically installed along with your package.
This means that you can’t assume the package is available
The cffr
(Hernangómez 2021) package will write a citation file in Citation File Format (CFF) (Druskat et al. 2021) from the description file.
🎬 Add a CFF file with:
cffr::cff_write()
Now would be a good time to commit your changes and push them to GitHub
pkgdown
sites (optional)DESCRIPTION
devtools
workflow functionsdevtools::document()
#'
@
use_package()
pkg::function()
library()
cffr::cff_write()
will write a citation file from the description file.