Bắt đầu ngayBắt đầu miễn phí

Function argument documentation

When documenting a function, one crucial aspect to cover is its arguments. With roxygen2, you can achieve this using the @param tag, followed by the argument name and relevant details. You'll just document the first argument in the exercise here, but it is best practice to document each of your function arguments, so you should think about how to document the other two at a later time.

Bài tập này là một phần của khóa học

Developing R Packages

Xem khóa học

Hướng dẫn bài tập

  • Add an appropriate tag to document the first argument of the dist_converter() function by adding the following details to this tag: A numerical distance value to be converted.

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

#' Convert between distances
#'
#' Performs the conversion based on specified `unit_from` and `unit_to` values.
#'
# Add appropriate tag and details to document the first argument
___
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.")
  }
}
Chỉnh sửa và Chạy Mã