safe_mean <- function(x, na.rm = TRUE) {
stopifnot(is.numeric(x))
mean(x, na.rm = na.rm)
}
safe_mean(c(1, 2, NA))[1] 1.5
safe_mean <- function(x, na.rm = TRUE) {
stopifnot(is.numeric(x))
mean(x, na.rm = na.rm)
}
safe_mean(c(1, 2, NA))[1] 1.5
robust_divide <- function(a, b) {
tryCatch(a / b,
warning = function(w) NA_real_,
error = function(e) NA_real_)
}
robust_divide(10, 0)[1] Inf
Install once: install.packages(c("testthat","devtools","usethis","roxygen2"))
usethis::use_testthat()
usethis::use_test("safe_mean")Create tests/testthat/test-safe_mean.R:
testthat::test_that("safe_mean works", {
x <- c(1,2,NA)
testthat::expect_equal(safe_mean(x), 1.5)
testthat::expect_error(safe_mean("oops"))
})Test passed 😸
#' Compute a safe mean
#' @param x Numeric vector
#' @param na.rm Logical; remove NAs
#' @return Numeric scalar
#' @examples
#' safe_mean(c(1,2,NA))
#' @export
safe_mean <- function(x, na.rm = TRUE) {
stopifnot(is.numeric(x))
mean(x, na.rm = na.rm)
}Run:
devtools::document()winsorize(x, probs=c(0.05,0.95)) and test it.validate_columns(df, required=c("USUBJID","AGE")) and add tests.