assert_date_var {admiraldev} | R Documentation |
Is a Variable in a Dataset a Date or Datetime Variable?
Description
Checks if a variable in a dataset is a date or datetime variable
Usage
assert_date_var(
dataset,
var,
dataset_name = rlang::caller_arg(dataset),
var_name = rlang::caller_arg(var),
message = NULL,
class = "assert_date_var",
call = parent.frame()
)
Arguments
dataset |
The dataset where the variable is expected |
var |
The variable to check |
dataset_name |
The name of the dataset. If the argument is specified, the specified name is displayed in the error message. |
var_name |
The name of the variable. If the argument is specified, the specified name is displayed in the error message. |
message |
( |
class |
Subclass of the condition. |
call |
The execution environment of a currently running
function, e.g. You only need to supply Can also be For more information about error calls, see Including function calls in error messages. |
Value
The function throws an error if var
is not a date or datetime variable in
dataset
and returns the input invisibly otherwise.
Examples
library(lubridate)
library(dplyr)
library(rlang)
example_fun <- function(dataset, var) {
var <- assert_symbol(enexpr(var))
assert_date_var(dataset = dataset, var = !!var)
}
my_data <- tribble(
~USUBJID, ~ADT,
"1", ymd("2020-12-06"),
"2", ymd("")
)
example_fun(
dataset = my_data,
var = ADT
)
try(example_fun(
dataset = my_data,
var = USUBJID
))
example_fun2 <- function(dataset, var) {
var <- assert_symbol(enexpr(var))
assert_date_var(
dataset = dataset,
var = !!var,
dataset_name = "your_data",
var_name = "your_var"
)
}
try(example_fun2(
dataset = my_data,
var = USUBJID
))