dbc
is designed to aid writing functions under the design by contract
philosophy, where function inputs and outputs are programmatically
asserted to adhere to specifications.
devtools::install_github(
"FinnishCancerRegistry/dbc",
ref = readline("enter latest tag on github: ")
)
# by adding arg assertion_type, you can use the same function for end-user
# purposes and internal purposes with clear error messages.
my_fun <- function(df, by, assertion_type = NULL) {
dbc::assert_is_character_nonNA_vector(
x = by,
assertion_type = assertion_type
)
dbc::assert_is_data_frame_with_required_names(
x = df,
required_names = by,
assertion_type = assertion_type
)
return(table(df[,by]))
}
my_fun(df, c("var_1", "var_2"))
my_fun_2 <- function(df) {
my_fun(df, c("var_1", "var_2"), assertion_type = "prod_input")
}