Function documentation starters
The roxygen2
headers, included in the same script as the function code, use roxygen2
comments #'
to identify the header lines. The first two comments (title and description) have special meaning and do not require tags, but must be separated by a new line. Keep in mind that the title provides a brief overview of the function's objective, while the description offers additional information and elaboration. For example:
#' My function title
#'
#' Its description
This exercise is part of the course
Developing R Packages
Exercise instructions
- Add the title "Convert between distances" to your
roxygen2
header. - Add the following short description of the function: "Performs the conversion based on specified
unit_from
andunit_to
values."
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Add the title
___
___
# Add the description
___
dist_converter <- function(dist_value, unit_from, unit_to) {
if (unit_from == "feet" && unit_to == "meters") {
return(dist_value / 3.28)
} else if (unit_from == "meters" && unit_to == "feet") {
return(dist_value * 3.28)
} else if (unit_from == unit_to) {
warning("unit_from and unit_to are the same, returning dist_value")
return(dist_value)
} else {
stop("This function only supports conversions between feet and meters.")
}
}