library(testthat) library(assertthat) library(dplyr)
This function returns the Body Mass Index of an individual (https://en.wikipedia.org/wiki/Body_mass_index).
The function must take 2 parameters, the mass in kg and the height in meter. It returns the result of the operation mass/(height * height).
#' bmi Body Mass Index of an individual #' #' @param mass in kg #' @param height in m #' @return bmi as mass/(height * height) #' @importFrom assertthat assert_that #' @importFrom dplyr mutate #' @export #' #' @examples bmi <- function(mass, height) { if(!(is.na(mass)|(is.numeric(mass)&mass>0))) {stop("mass should be numeric and >0")} if(!(is.na(height)|(is.numeric(height)&height>0))) {stop("height should be numeric and >0")} bmi <- NA if (!is.na(mass)&!is.na(height)) { bmi <- mass/(height * height) } return(bmi) }
bmi(mass = 80, height = 1.80) bmi(mass = NA, height = 1.80) bmi(mass = 80, height = NA) bmi(mass = NA, height = NA)
library(dplyr) test_that("bmi works", { test_that("1. not numeric", { expect_error(bmi(mass = 'toto', height = 1.80)) }) test_that("2. NA ok", { expect_equal(bmi(mass = NA, height = 1.80),NA) }) })
# Run but keep eval=FALSE to avoid infinite loop # Execute in the console directly fusen::inflate(flat_file = "dev/flat_minimal.Rmd", vignette_name = "Minimal")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.