Developing R Packages
Jasmin Ludolf
Content Developer
C_value <- 35
F <- 9/5 * C_value + 32
F
[1] 95
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.") } }
temp_converter(100)
[1] 212
temp_converter(95, unit_from = "Fahrenheit", unit_to = "Celsius")
[1] 35
dump("temp_converter",
file = "R/temp_converter.R")
devtools::install()
- checking for file 'DESCRIPTION' ...
- preparing 'unitConverter':
- checking DESCRIPTION meta-information ...
--install-tests
* installing to library
* installing *source* package 'unitConverter' ...
** R
** data
** inst
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
* DONE (unitConverter)
value | unit |
---|---|
36.26 | Celsius |
library(unitConverter)
unitConverter:::temp_converter(
value = temperature_data$value[1], unit_from = temperature_data$unit[1], unit_to = "Fahrenheit")
[1] 97.268
Developing R Packages