Developing R Packages
Jasmin Ludolf
Content Developer
temp_converter <- function(value, unit_from = "Celsius", unit_to = "Fahrenheit") {
if (unit_from == "Celsius" && unit_to == "Fahrenheit") {
return(value * 9/5 + 32)
} else if (unit_from == "Fahrenheit" && unit_to == "Celsius") {
return((value - 32) * 5/9)
} else if (unit_from == unit_to) {
warning("unit_from and unit_to are the same, value returned.")
return(value)
} else {
stop("Invalid unit_from or unit_to. Only 'Celsius' and 'Fahrenheit'
are accepted.")
}
}
#' Convert between Fahrenheit and Celsius temperatures #'
#' `temp_converter()` performs the conversion based on the specified `unit_from` #' and `unit_to` values. If the conversion is not possible due to invalid units or #' if `unit_from` and `unit_to` are the same, appropriate warnings or errors #' are raised.
temp_converter <- function(value, unit_from = "Celsius", unit_to = "Fahrenheit")
#' @param value A numerical value of the temperature to be converted.
#' @param unit_from A character string of the temperature unit to convert from
#' (default is "Celsius").
#' @param unit_to A character string of the temperature unit to convert to
#' (default is "Fahrenheit").
#' @returns A numeric temperature value in the unit specified as `unit_to`.
#' @export
#' @examples
#' # Convert 25 degrees Celsius to Fahrenheit
#' temp_converter(25, unit_from = "Celsius", unit_to = "Fahrenheit")
#' # Convert 100 degrees Fahrenheit to Celsius
#' temp_converter(100, unit_from = "Fahrenheit", unit_to = "Celsius")
Practical usage
Improves comprehension
Encourages exploration
Reinforces understanding
Update the NAMESPACE
file
library(roxygen2)
roxygenize()
`# Generated by roxygen2: do not edit
'# by hand
export(temp_converter)
Developing R Packages