CommencerCommencer gratuitement

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

Cet exercice fait partie du cours

Developing R Packages

Afficher le cours

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 and unit_to values."

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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.")
  }
}
Modifier et exécuter le code