Skip to contents

Checks if an argument is a data frame and (optionally) whether is contains a set of required variables

Usage

assert_data_frame(
  arg,
  required_vars = NULL,
  check_is_grouped = TRUE,
  optional = FALSE
)

Arguments

arg

A function argument to be checked

required_vars

A list of variables created using exprs()

check_is_grouped

Throw an error is dataset is grouped? Defaults to TRUE.

optional

Is the checked argument optional? If set to FALSE and arg is NULL then an error is thrown

Value

The function throws an error if arg is not a data frame or if arg

is a data frame but misses any variable specified in required_vars. Otherwise, the input is returned invisibly.

Examples

library(pharmaversesdtm)
library(dplyr, warn.conflicts = FALSE)
library(rlang)
data(dm)

example_fun <- function(dataset) {
  assert_data_frame(dataset, required_vars = exprs(STUDYID, USUBJID))
}

example_fun(dm)

try(example_fun(select(dm, -STUDYID)))
#> Error in assert_data_frame(dataset, required_vars = exprs(STUDYID, USUBJID)) : 
#>   Required variable `STUDYID` is missing

try(example_fun("Not a dataset"))
#> Error in assert_data_frame(dataset, required_vars = exprs(STUDYID, USUBJID)) : 
#>   `dataset` must be a data frame but is `"Not a dataset"`