开始使用免费开始使用

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

本练习是课程的一部分

Developing R Packages

查看课程

练习说明

  • 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."

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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.")
  }
}
编辑并运行代码