| Title: | Utility Functions and Development Tools for the Admiral Package Family |
|---|---|
| Description: | Utility functions to check data, variables and conditions for functions used in 'admiral' and 'admiral' extension packages. Additional utility helper functions to assist developers with maintaining documentation, testing and general upkeep of 'admiral' and 'admiral' extension packages. |
| Authors: | Edoardo Mancini [aut, cre] (ORCID: <https://orcid.org/0009-0006-4899-8641>), Stefan Bundfuss [aut] (ORCID: <https://orcid.org/0009-0005-0027-1198>), Arianna Cascone [aut] (ORCID: <https://orcid.org/0000-0001-5948-2831>), Kristin Dahnert [aut], Jeffrey Dickinson [aut], Ross Farrugia [aut], Fanny Gautier [aut] (ORCID: <https://orcid.org/0009-0004-3581-0131>), Gordon Miller [aut], Lina Patil [aut], Ben Straub [aut], F. Hoffmann-La Roche AG [cph, fnd], GlaxoSmithKline LLC [cph, fnd] |
| Maintainer: | Edoardo Mancini <[email protected]> |
| License: | Apache License (>= 2) |
| Version: | 1.4.1.9001 |
| Built: | 2026-06-03 08:39:58 UTC |
| Source: | https://github.com/pharmaverse/admiraldev |
Returns a logical vector indicating if there is no match of the
left operand in the right operand.
x %notin% tablex %notin% table
x |
The values to be matched
|
table |
The values to be matched against
|
A logical vector
Developer Utility Functions:
contains_vars(),
convert_dtm_to_dtc(),
extract_vars(),
filter_if(),
vars2chr()
This function is deprecated. Please get in touch if you are using this function!
lhs %or% rhslhs %or% rhs
lhs |
Any valid R expression
|
rhs |
Any valid R expression
|
The function evaluates the expression lhs and if this expression results
in an error, it catches that error and proceeds with evaluating the expression
rhs and returns that result.
Either the result of evaluating lhs, rhs or an error
Other deprecated:
arg_name(),
enumerate(),
friendly_type_of(),
valid_time_units(),
what_is_it()
Add a suffix to variables in a list of expressions
add_suffix_to_vars(order, vars, suffix)add_suffix_to_vars(order, vars, suffix)
order |
List of expressions
|
vars |
Variables to change
|
suffix |
Suffix
|
The list of expression where for each element the suffix (suffix) is
added to every symbol specified for vars
Helpers for working with Quosures:
expr_c(),
replace_symbol_in_expr(),
replace_values_by_names()
library(dplyr, warn.conflicts = FALSE) library(rlang) add_suffix_to_vars(exprs(ADT, desc(AVAL), AVALC), vars = exprs(AVAL), suffix = ".join")library(dplyr, warn.conflicts = FALSE) library(rlang) add_suffix_to_vars(exprs(ADT, desc(AVAL), AVALC), vars = exprs(AVAL), suffix = ".join")
This function is deprecated, please use rlang::caller_arg() instead.
arg_name(expr)arg_name(expr)
expr |
An expression created inside a function using
|
character vector
Other deprecated:
%or%,
enumerate(),
friendly_type_of(),
valid_time_units(),
what_is_it()
Checks if an argument is an atomic vector
assert_atomic_vector( arg, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_atomic_vector", call = parent.frame() )assert_atomic_vector( arg, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_atomic_vector", call = parent.frame() )
arg |
A function argument to be checked
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if arg is not an atomic vector.
Otherwise, the input is returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(x) { assert_atomic_vector(x) } example_fun(1:10) try(example_fun(list(1, 2)))example_fun <- function(x) { assert_atomic_vector(x) } example_fun(1:10) try(example_fun(list(1, 2)))
Checks if an argument is a character scalar and (optionally) whether it matches
one of the provided values.
assert_character_scalar( arg, values = NULL, case_sensitive = TRUE, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_character_scalar", call = parent.frame() )assert_character_scalar( arg, values = NULL, case_sensitive = TRUE, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_character_scalar", call = parent.frame() )
arg |
A function argument to be checked
|
values |
A
|
case_sensitive |
Should the argument be handled case-sensitive?
If set to
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if arg is not a character vector or if arg
is a character vector but of length > 1 or if its value is not one of the values
specified. Otherwise, the input is returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(msg_type) { assert_character_scalar(msg_type, values = c("warning", "error")) } example_fun("warning") try(example_fun("message")) try(example_fun(TRUE)) # handling arguments case-insensitive example_fun2 <- function(msg_type) { msg_type <- assert_character_scalar( msg_type, values = c("warning", "error"), case_sensitive = FALSE ) if (msg_type == "warning") { print("A warning was requested.") } } example_fun2("Warning")example_fun <- function(msg_type) { assert_character_scalar(msg_type, values = c("warning", "error")) } example_fun("warning") try(example_fun("message")) try(example_fun(TRUE)) # handling arguments case-insensitive example_fun2 <- function(msg_type) { msg_type <- assert_character_scalar( msg_type, values = c("warning", "error"), case_sensitive = FALSE ) if (msg_type == "warning") { print("A warning was requested.") } } example_fun2("Warning")
Checks if an argument is a character vector
assert_character_vector( arg, values = NULL, named = FALSE, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_character_vector", call = parent.frame() )assert_character_vector( arg, values = NULL, named = FALSE, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_character_vector", call = parent.frame() )
arg |
A function argument to be checked
|
values |
A
|
named |
If set to
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if arg is not a character vector or if
any element is not included in the list of valid values. Otherwise, the input
is returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(chr) { assert_character_vector(chr) } example_fun(letters) try(example_fun(1:10)) example_fun2 <- function(chr) { assert_character_vector(chr, named = TRUE) } try(example_fun2(c(alpha = "a", "b", gamma = "c")))example_fun <- function(chr) { assert_character_vector(chr) } example_fun(letters) try(example_fun(1:10)) example_fun2 <- function(chr) { assert_character_vector(chr, named = TRUE) } try(example_fun2(c(alpha = "a", "b", gamma = "c")))
Checks if an argument is a data frame and (optionally) whether is contains a set of required variables
assert_data_frame( arg, required_vars = NULL, check_is_grouped = TRUE, check_is_rowwise = TRUE, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_data_frame", call = parent.frame() )assert_data_frame( arg, required_vars = NULL, check_is_grouped = TRUE, check_is_rowwise = TRUE, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_data_frame", call = parent.frame() )
arg |
A function argument to be checked
|
required_vars |
A list of variables created using
|
check_is_grouped |
Throws an error if
|
check_is_rowwise |
Throws an error if
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
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.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
library(dplyr) library(rlang) dm <- tribble( ~STUDYID, ~USUBJID, "XYZ", "1", "XYZ", "2" ) example_fun <- function(dataset) { assert_data_frame(dataset, required_vars = exprs(STUDYID, USUBJID)) } example_fun(dm) try(example_fun(select(dm, -STUDYID))) try(example_fun("Not a dataset")) try(example_fun(group_by(dm, USUBJID)))library(dplyr) library(rlang) dm <- tribble( ~STUDYID, ~USUBJID, "XYZ", "1", "XYZ", "2" ) example_fun <- function(dataset) { assert_data_frame(dataset, required_vars = exprs(STUDYID, USUBJID)) } example_fun(dm) try(example_fun(select(dm, -STUDYID))) try(example_fun("Not a dataset")) try(example_fun(group_by(dm, USUBJID)))
Checks if a variable in a dataset is a date or datetime variable
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() )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() )
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. |
The function throws an error if var is not a date or datetime variable in
dataset and returns the input invisibly otherwise.
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 ))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 ))
Check if an object/vector is a date or datetime variable without needing a dataset as input
assert_date_vector( arg, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_date_vector", call = parent.frame() )assert_date_vector( arg, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_date_vector", call = parent.frame() )
arg |
The function argument to be checked
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function returns an error if arg is missing, or not a date or datetime variable
but otherwise returns an invisible output.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(arg) { assert_date_vector(arg) } example_fun( as.Date("2022-01-30", tz = "UTC") ) try(example_fun("1993-07-14"))example_fun <- function(arg) { assert_date_vector(arg) } example_fun( as.Date("2022-01-30", tz = "UTC") ) try(example_fun("1993-07-14"))
Assert Argument is an Expression
assert_expr( arg, optional = FALSE, arg_name = gsub("^enexpr\\((.*)\\)$", "\\1", rlang::caller_arg(arg)), message = NULL, class = "assert_expr", call = parent.frame() )assert_expr( arg, optional = FALSE, arg_name = gsub("^enexpr\\((.*)\\)$", "\\1", rlang::caller_arg(arg)), message = NULL, class = "assert_expr", call = parent.frame() )
arg |
A function argument to be checked
|
optional |
Is the checked argument optional? If set to
|
arg_name |
By default the expression specified for
|
message |
string passed to
|
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. |
The function throws an error if arg is not an expression, i.e. either
a symbol or a call, or returns the input invisibly otherwise
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
Checks if the argument is a list of expressions.
assert_expr_list( arg, required_elements = NULL, named = FALSE, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_expr_list", call = parent.frame() )assert_expr_list( arg, required_elements = NULL, named = FALSE, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_expr_list", call = parent.frame() )
arg |
A function argument to be checked
|
required_elements |
A
|
named |
If set to
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if arg is not a list of expressions.
Otherwise, the input it returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
library(rlang) example_fun <- function(vars) { assert_expr_list(vars) } example_fun(exprs(DTHDOM = "AE", DTHSEQ = AESEQ)) try(example_fun(exprs("AE", DTSEQ = AESEQ, !!list("a"), !!list("a"))))library(rlang) example_fun <- function(vars) { assert_expr_list(vars) } example_fun(exprs(DTHDOM = "AE", DTHSEQ = AESEQ)) try(example_fun(exprs("AE", DTSEQ = AESEQ, !!list("a"), !!list("a"))))
Is an Argument a Filter Condition?
assert_filter_cond( arg, optional = FALSE, arg_name = gsub("^enexpr\\((.*)\\)$", "\\1", rlang::caller_arg(arg)), message = NULL, class = "assert_filter_cond", call = parent.frame() )assert_filter_cond( arg, optional = FALSE, arg_name = gsub("^enexpr\\((.*)\\)$", "\\1", rlang::caller_arg(arg)), message = NULL, class = "assert_filter_cond", call = parent.frame() )
arg |
Quosure - filtering condition.
|
optional |
Logical - is the argument optional?
|
arg_name |
By default the expression specified for
|
message |
string passed to
|
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. |
Check if arg is a suitable filtering condition to be used in
functions like subset or dplyr::filter.
Performs necessary checks and returns arg if all pass.
Otherwise throws an informative error.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
library(dplyr, warn.conflicts = FALSE) library(rlang) dm <- dplyr::tribble( ~DOMAIN, ~STUDYID, ~USUBJID, ~AGE, "DM", "STUDY X", "01-701-1015", 64, "DM", "STUDY X", "01-701-1016", 65, ) # typical usage in a function as an argument check example_fun <- function(dat, x) { x <- assert_filter_cond(enexpr(x), arg_name = "x") filter(dat, !!x) } example_fun(dm, AGE == 64) try(assert_filter_cond(mtcars))library(dplyr, warn.conflicts = FALSE) library(rlang) dm <- dplyr::tribble( ~DOMAIN, ~STUDYID, ~USUBJID, ~AGE, "DM", "STUDY X", "01-701-1015", 64, "DM", "STUDY X", "01-701-1016", 65, ) # typical usage in a function as an argument check example_fun <- function(dat, x) { x <- assert_filter_cond(enexpr(x), arg_name = "x") filter(dat, !!x) } example_fun(dm, AGE == 64) try(assert_filter_cond(mtcars))
Checks if the argument is a function and if all expected arguments are provided by the function.
assert_function( arg, params = NULL, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_function", call = parent.frame() )assert_function( arg, params = NULL, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_function", call = parent.frame() )
arg |
A function The function to be checked
|
params |
A character vector A character vector of expected argument names for the aforementioned function in
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error
if the argument is not a function or
if the function does not provide all arguments as specified for the
params argument (assuming ellipsis is not in function formals)
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(fun) { assert_function(fun, params = c("x")) } example_fun(mean) try(example_fun(1)) try(example_fun(sum))example_fun <- function(fun) { assert_function(fun, params = c("x")) } example_fun(mean) try(example_fun(1)) try(example_fun(sum))
Checks if an argument is an integer scalar
assert_integer_scalar( arg, subset = "none", optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_integer_scalar", call = parent.frame() )assert_integer_scalar( arg, subset = "none", optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_integer_scalar", call = parent.frame() )
arg |
A function argument to be checked
|
subset |
A subset of integers that
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if arg is not an integer belonging to the
specified subset. Otherwise, the input is returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(num1, num2) { assert_integer_scalar(num1, subset = "positive") assert_integer_scalar(num2, subset = "negative") } example_fun(1, -9) try(example_fun(1.5, -9)) try(example_fun(2, 0)) try(example_fun("2", 0))example_fun <- function(num1, num2) { assert_integer_scalar(num1, subset = "positive") assert_integer_scalar(num2, subset = "negative") } example_fun(1, -9) try(example_fun(1.5, -9)) try(example_fun(2, 0)) try(example_fun("2", 0))
Checks if the elements of a list of named lists/classes fulfill a certain condition. If not, an error is issued and all elements of the list not fulfilling the condition are listed.
assert_list_element( list, element, condition, message_text, arg_name = rlang::caller_arg(list), message = NULL, class = "assert_list_element", call = parent.frame(), ... )assert_list_element( list, element, condition, message_text, arg_name = rlang::caller_arg(list), message = NULL, class = "assert_list_element", call = parent.frame(), ... )
list |
A list to be checked A list of named lists or classes is expected.
|
element |
The name of an element of the lists/classes A character scalar is expected.
|
condition |
Condition to be fulfilled
The condition is evaluated for each element of the list. The element of the
lists/classes can be referred to by its name, e.g.,
|
message_text |
Text to be displayed in the error message above
the listing of values that do not meet the condition.
The text should describe the condition to be fulfilled,
e.g.,
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
... |
Objects required to evaluate the condition or the message text If the condition or the message text contains objects apart from the element, they have to be passed to the function. See the second example below.
|
An error if the condition is not met. The input otherwise.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
death <- list( dataset_name = "adsl", date = "DTHDT", censor = 0 ) lstalv <- list( dataset_name = "adsl", date = "LSTALVDT", censor = 1 ) events <- list(death, lstalv) try(assert_list_element( list = events, element = "censor", condition = censor == 0, message_text = "For events the censor values must be zero." )) try(assert_list_element( list = events, element = "dataset_name", condition = dataset_name %in% c("adrs", "adae"), valid_datasets = c("adrs", "adae"), message_text = paste( "The dataset name must be one of the following: {.val {valid_datasets}}" ) ))death <- list( dataset_name = "adsl", date = "DTHDT", censor = 0 ) lstalv <- list( dataset_name = "adsl", date = "LSTALVDT", censor = 1 ) events <- list(death, lstalv) try(assert_list_element( list = events, element = "censor", condition = censor == 0, message_text = "For events the censor values must be zero." )) try(assert_list_element( list = events, element = "dataset_name", condition = dataset_name %in% c("adrs", "adae"), valid_datasets = c("adrs", "adae"), message_text = paste( "The dataset name must be one of the following: {.val {valid_datasets}}" ) ))
Checks if an argument is a list of objects inheriting from the S3 class or type specified.
assert_list_of( arg, cls, named = FALSE, optional = TRUE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_list_of", call = parent.frame() )assert_list_of( arg, cls, named = FALSE, optional = TRUE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_list_of", call = parent.frame() )
arg |
A function argument to be checked
|
cls |
The S3 class or type to check for
|
named |
If set to
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if arg is not a list or if arg is a list but
its elements are not objects inheriting from class or of type class.
Otherwise, the input is returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(list) { assert_list_of(list, "data.frame") } example_fun(list(mtcars, iris)) try(example_fun(list(letters, 1:10))) try(example_fun(c(TRUE, FALSE))) example_fun2 <- function(list) { assert_list_of(list, "numeric", named = TRUE) } try(example_fun2(list(1, 2, 3, d = 4)))example_fun <- function(list) { assert_list_of(list, "data.frame") } example_fun(list(mtcars, iris)) try(example_fun(list(letters, 1:10))) try(example_fun(c(TRUE, FALSE))) example_fun2 <- function(list) { assert_list_of(list, "numeric", named = TRUE) } try(example_fun2(list(1, 2, 3, d = 4)))
Checks if an argument is a logical scalar
assert_logical_scalar( arg, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_logical_scalar", call = parent.frame() )assert_logical_scalar( arg, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_logical_scalar", call = parent.frame() )
arg |
A function argument to be checked
|
optional |
Is the checked argument optional?
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if arg is neither TRUE or FALSE. Otherwise,
the input is returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(flag) { assert_logical_scalar(flag) } example_fun(FALSE) try(example_fun(NA)) try(example_fun(c(TRUE, FALSE, FALSE))) try(example_fun(1:10))example_fun <- function(flag) { assert_logical_scalar(flag) } example_fun(FALSE) try(example_fun(NA)) try(example_fun(c(TRUE, FALSE, FALSE))) try(example_fun(1:10))
Assert that all elements of the argument are named.
assert_named( arg, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_named", call = parent.frame() )assert_named( arg, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_named", call = parent.frame() )
arg |
A function argument to be checked
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if arg is not a named list or vector or
returns the input invisibly otherwise
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(varval_list) { assert_named(varval_list) } example_fun(list(var1 = 1, var2 = "x")) try(example_fun(list(1, "x"))) try(example_fun(list(var = 1, "x")))example_fun <- function(varval_list) { assert_named(varval_list) } example_fun(list(var1 = 1, var2 = "x")) try(example_fun(list(1, "x"))) try(example_fun(list(var = 1, "x")))
Checks if an argument is a numeric vector
assert_numeric_vector( arg, length = NULL, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_numeric_vector", call = parent.frame() )assert_numeric_vector( arg, length = NULL, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_numeric_vector", call = parent.frame() )
arg |
A function argument to be checked
|
length |
Expected length If the argument is not specified or set to
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if arg is not a numeric vector.
Otherwise, the input is returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(num) { assert_numeric_vector(num) } example_fun(1:10) try(example_fun(letters)) example_fun <- function(num) { assert_numeric_vector(num, length = 2) } try(example_fun(1:10))example_fun <- function(num) { assert_numeric_vector(num) } example_fun(1:10) try(example_fun(letters)) example_fun <- function(num) { assert_numeric_vector(num, length = 2) } try(example_fun(1:10))
Checks if there is a one to one mapping between two lists of variables.
assert_one_to_one( dataset, vars1, vars2, dataset_name = rlang::caller_arg(dataset), message = NULL, class = "assert_one_to_one", call = parent.frame() )assert_one_to_one( dataset, vars1, vars2, dataset_name = rlang::caller_arg(dataset), message = NULL, class = "assert_one_to_one", call = parent.frame() )
dataset |
Dataset to be checked The variables specified for
|
vars1 |
First list of variables
|
vars2 |
Second list of variables
|
dataset_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
An error if the condition is not meet. The input otherwise.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
library(dplyr) library(rlang) df <- tribble( ~SPECIES, ~SPECIESN, "DOG", 1L, "CAT", 2L, "DOG", 1L ) assert_one_to_one(df, vars1 = exprs(SPECIES), vars2 = exprs(SPECIESN)) df_many <- tribble( ~SPECIES, ~SPECIESN, "DOG", 1L, "CAT", 2L, "DOG", 3L ) try( assert_one_to_one(df_many, vars1 = exprs(SPECIES), vars2 = exprs(SPECIESN)) ) try( assert_one_to_one(df_many, vars1 = exprs(SPECIESN), vars2 = exprs(SPECIES)) )library(dplyr) library(rlang) df <- tribble( ~SPECIES, ~SPECIESN, "DOG", 1L, "CAT", 2L, "DOG", 1L ) assert_one_to_one(df, vars1 = exprs(SPECIES), vars2 = exprs(SPECIESN)) df_many <- tribble( ~SPECIES, ~SPECIESN, "DOG", 1L, "CAT", 2L, "DOG", 3L ) try( assert_one_to_one(df_many, vars1 = exprs(SPECIES), vars2 = exprs(SPECIESN)) ) try( assert_one_to_one(df_many, vars1 = exprs(SPECIESN), vars2 = exprs(SPECIES)) )
Checks if a parameter (PARAMCD) does not exist in a dataset.
assert_param_does_not_exist( dataset, param, arg_name = rlang::caller_arg(dataset), message = NULL, class = "assert_param_does_not_exist", call = parent.frame() )assert_param_does_not_exist( dataset, param, arg_name = rlang::caller_arg(dataset), message = NULL, class = "assert_param_does_not_exist", call = parent.frame() )
dataset |
A
|
param |
Parameter code to check
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if the parameter exists in the input dataset. Otherwise, the dataset is returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
library(dplyr) advs <- tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, "P02", "WEIGHT", 85.7, "kg", "WEIGHT", 85.7 ) assert_param_does_not_exist(advs, param = "HR") try(assert_param_does_not_exist(advs, param = "WEIGHT"))library(dplyr) advs <- tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, "P02", "WEIGHT", 85.7, "kg", "WEIGHT", 85.7 ) assert_param_does_not_exist(advs, param = "HR") try(assert_param_does_not_exist(advs, param = "WEIGHT"))
Checks if an argument is an object inheriting from the S3 class specified.
assert_s3_class( arg, cls, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_s3_class", call = parent.frame() )assert_s3_class( arg, cls, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_s3_class", call = parent.frame() )
arg |
A function argument to be checked
|
cls |
The S3 class to check for
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if arg is an object which does not inherit from class.
Otherwise, the input is returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(obj) { assert_s3_class(obj, "factor") } example_fun(as.factor(letters)) try(example_fun(letters)) try(example_fun(1:10))example_fun <- function(obj) { assert_s3_class(obj, "factor") } example_fun(as.factor(letters)) try(example_fun(letters)) try(example_fun(1:10))
Checks if all arguments are of the same type.
assert_same_type( ..., .message = c("Arguments {.arg {arg_names}} must be the same type.", i = paste("Argument types are", paste0("{.arg ", arg_names, "} {.cls ", types, "}", collapse = ", "))), .class = "assert_same_type", .call = parent.frame() )assert_same_type( ..., .message = c("Arguments {.arg {arg_names}} must be the same type.", i = paste("Argument types are", paste0("{.arg ", arg_names, "} {.cls ", types, "}", collapse = ", "))), .class = "assert_same_type", .call = parent.frame() )
... |
Arguments to be checked
|
.message |
character vector passed to
|
.class |
character vector passed to
|
.call |
environment passed to
|
The function throws an error if not all arguments are of the same type.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_symbol(),
assert_unit(),
assert_vars(),
assert_varval_list()
example_fun <- function(true_value, false_value, missing_value) { assert_same_type(true_value, false_value, missing_value) } example_fun( true_value = "Y", false_value = "N", missing_value = NA_character_ ) try(example_fun( true_value = 1, false_value = 0, missing_value = "missing" ))example_fun <- function(true_value, false_value, missing_value) { assert_same_type(true_value, false_value, missing_value) } example_fun( true_value = "Y", false_value = "N", missing_value = NA_character_ ) try(example_fun( true_value = 1, false_value = 0, missing_value = "missing" ))
Checks if an argument is a symbol
assert_symbol( arg, optional = FALSE, arg_name = gsub("^enexpr\\((.*)\\)$", "\\1", rlang::caller_arg(arg)), message = NULL, class = "assert_symbol", call = parent.frame() )assert_symbol( arg, optional = FALSE, arg_name = gsub("^enexpr\\((.*)\\)$", "\\1", rlang::caller_arg(arg)), message = NULL, class = "assert_symbol", call = parent.frame() )
arg |
A function argument to be checked. Must be a
|
optional |
Is the checked argument optional? If set to
|
arg_name |
By default the expression specified for
|
message |
string passed to
|
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. |
The function throws an error if arg is not a symbol and returns the input
invisibly otherwise.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_unit(),
assert_vars(),
assert_varval_list()
library(dplyr, warn.conflicts = FALSE) library(rlang) dm <- dplyr::tribble( ~DOMAIN, ~USUBJID, "DM", "01-701-1015", "DM", "01-701-1016", ) example_fun <- function(dat, var) { var <- assert_symbol(enexpr(var)) select(dat, !!var) } example_fun(dm, USUBJID) try(example_fun(dm)) try(example_fun(dm, "USUBJID")) try(example_fun(dm, toupper(PARAMCD)))library(dplyr, warn.conflicts = FALSE) library(rlang) dm <- dplyr::tribble( ~DOMAIN, ~USUBJID, "DM", "01-701-1015", "DM", "01-701-1016", ) example_fun <- function(dat, var) { var <- assert_symbol(enexpr(var)) select(dat, !!var) } example_fun(dm, USUBJID) try(example_fun(dm)) try(example_fun(dm, "USUBJID")) try(example_fun(dm, toupper(PARAMCD)))
Checks if a parameter (PARAMCD) in a dataset is provided in the expected
unit.
assert_unit( dataset, param, required_unit = NULL, get_unit_expr, arg_name = rlang::caller_arg(required_unit), message = NULL, class = "assert_unit", call = parent.frame() )assert_unit( dataset, param, required_unit = NULL, get_unit_expr, arg_name = rlang::caller_arg(required_unit), message = NULL, class = "assert_unit", call = parent.frame() )
dataset |
Dataset to be checked The variable
|
param |
Parameter code of the parameter to check
|
required_unit |
Expected unit(s) If the argument is set to
|
get_unit_expr |
Expression used to provide the unit of
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error
if there is more than one non-missing unit in the dataset or
if the unit variable differs from the expected unit for any observation of the parameter in the input dataset.
Otherwise, the dataset is returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_vars(),
assert_varval_list()
library(dplyr) advs <- tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, "P02", "WEIGHT", 85.7, "kg", "WEIGHT", 85.7 ) assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSSTRESU) try( assert_unit( advs, param = "WEIGHT", required_unit = c("g", "mg"), get_unit_expr = VSSTRESU ) ) # Checking uniqueness of unit only advs <- tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, "P02", "WEIGHT", 85700, "g", "WEIGHT", 85700 ) try( assert_unit(advs, param = "WEIGHT", get_unit_expr = VSSTRESU) )library(dplyr) advs <- tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, "P02", "WEIGHT", 85.7, "kg", "WEIGHT", 85.7 ) assert_unit(advs, param = "WEIGHT", required_unit = "kg", get_unit_expr = VSSTRESU) try( assert_unit( advs, param = "WEIGHT", required_unit = c("g", "mg"), get_unit_expr = VSSTRESU ) ) # Checking uniqueness of unit only advs <- tribble( ~USUBJID, ~VSTESTCD, ~VSTRESN, ~VSSTRESU, ~PARAMCD, ~AVAL, "P01", "WEIGHT", 80.1, "kg", "WEIGHT", 80.1, "P02", "WEIGHT", 85700, "g", "WEIGHT", 85700 ) try( assert_unit(advs, param = "WEIGHT", get_unit_expr = VSSTRESU) )
Checks if an argument is a valid list of symbols (e.g., created by exprs())
assert_vars( arg, expect_names = FALSE, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_vars", call = parent.frame() )assert_vars( arg, expect_names = FALSE, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_vars", call = parent.frame() )
arg |
A function argument to be checked
|
expect_names |
If the argument is set to
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if arg is not a list of symbols (e.g., created
by exprs() and returns the input invisibly otherwise.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_varval_list()
library(dplyr, warn.conflicts = FALSE) library(rlang) example_fun <- function(by_vars) { assert_vars(by_vars) } example_fun(exprs(USUBJID, PARAMCD)) try(example_fun(quos(USUBJID, PARAMCD))) try(example_fun(c("USUBJID", "PARAMCD", "VISIT"))) try(example_fun(exprs(USUBJID, toupper(PARAMCD), desc(AVAL)))) example_fun_name <- function(by_vars) { assert_vars(by_vars, expect_names = TRUE) } example_fun_name(exprs(APERSDT = APxxSDT, APEREDT = APxxEDT)) try(example_fun_name(exprs(APERSDT = APxxSDT, APxxEDT)))library(dplyr, warn.conflicts = FALSE) library(rlang) example_fun <- function(by_vars) { assert_vars(by_vars) } example_fun(exprs(USUBJID, PARAMCD)) try(example_fun(quos(USUBJID, PARAMCD))) try(example_fun(c("USUBJID", "PARAMCD", "VISIT"))) try(example_fun(exprs(USUBJID, toupper(PARAMCD), desc(AVAL)))) example_fun_name <- function(by_vars) { assert_vars(by_vars, expect_names = TRUE) } example_fun_name(exprs(APERSDT = APxxSDT, APEREDT = APxxEDT)) try(example_fun_name(exprs(APERSDT = APxxSDT, APxxEDT)))
Checks if the argument is a list of expressions where the expressions are
variable-value pairs. The value can be a symbol, a string, a numeric, an
expression, or NA.
assert_varval_list( arg, required_elements = NULL, accept_expr = TRUE, accept_var = FALSE, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_varval_list", call = parent.frame() )assert_varval_list( arg, required_elements = NULL, accept_expr = TRUE, accept_var = FALSE, optional = FALSE, arg_name = rlang::caller_arg(arg), message = NULL, class = "assert_varval_list", call = parent.frame() )
arg |
A function argument to be checked
|
required_elements |
A
|
accept_expr |
Should expressions on the right hand side be accepted?
|
accept_var |
Should unnamed variable names (e.g.
|
optional |
Is the checked argument optional? If set to
|
arg_name |
string indicating the label/symbol of the object being checked.
|
message |
string passed to
|
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. |
The function throws an error if arg is not a list of variable-value expressions.
Otherwise, the input it returned invisibly.
Checks for valid input and returns warning or errors messages:
assert_atomic_vector(),
assert_character_scalar(),
assert_character_vector(),
assert_data_frame(),
assert_date_vector(),
assert_expr(),
assert_expr_list(),
assert_filter_cond(),
assert_function(),
assert_integer_scalar(),
assert_list_element(),
assert_list_of(),
assert_logical_scalar(),
assert_named(),
assert_numeric_vector(),
assert_one_to_one(),
assert_param_does_not_exist(),
assert_s3_class(),
assert_same_type(),
assert_symbol(),
assert_unit(),
assert_vars()
library(dplyr, warn.conflicts = FALSE) library(rlang) example_fun <- function(vars) { assert_varval_list(vars) } example_fun(exprs(DTHDOM = "AE", DTHSEQ = AESEQ)) try(example_fun(exprs("AE", DTSEQ = AESEQ)))library(dplyr, warn.conflicts = FALSE) library(rlang) example_fun <- function(vars) { assert_varval_list(vars) } example_fun(exprs(DTHDOM = "AE", DTHSEQ = AESEQ)) try(example_fun(exprs("AE", DTSEQ = AESEQ)))
Wrap a String in Backquotes
backquote(x)backquote(x)
x |
A
|
A character vector
Helpers for working with Quotes and Quoting:
dquote(),
squote()
The function captures both output and expected messages from an R expression. If the expression results in an unexpected message, an error is issued.
capture_output(expr, srcref = NULL, expected_cnds = NULL, env = caller_env())capture_output(expr, srcref = NULL, expected_cnds = NULL, env = caller_env())
expr |
An R expression to evaluate
|
srcref |
The source reference of the expression
|
expected_cnds |
A character vector of expected conditions If the expression issues a condition of a class that is in this vector, the condition is ignored but added to the return value. Otherwise, an error is issued.
|
env |
The environment in which to evaluate the expression
|
A character vector of captured output and messages
capture_output(1 + 1) #> [1] "[1] 2" capture_output(log(-1)) #> Error in capture_output(log(-1)) : The expression #> > log(-1) #> issued an unexpected condition: #> NaNs produced #> If this is expected, add any of the classes "simpleWarning", "warning", and #> "condition" to the argument `expected_cnds`. capture_output(log(-1), expected_cnds = "warning") #> [1] "[1] NaN" "Warning in log(-1) : NaNs produced"
exprs() or
Source Variables from a List of Expressionscheck that argument contains valid variable(s) created with exprs() or
Source Variables from a List of Expressions
contains_vars(arg)contains_vars(arg)
arg |
A function argument to be checked
|
A TRUE if variables were valid variable
Developer Utility Functions:
%notin%,
convert_dtm_to_dtc(),
extract_vars(),
filter_if(),
vars2chr()
Helper Function to Convert Date (or Date-time) Objects to Characters of dtc Format (-DTC type of variable)
convert_dtm_to_dtc(dtm)convert_dtm_to_dtc(dtm)
dtm |
date or date-time
|
character vector
Developer Utility Functions:
%notin%,
contains_vars(),
extract_vars(),
filter_if(),
vars2chr()
Output a dataset in a vignette with the pre-specified admiral format.
dataset_vignette(dataset, display_vars = NULL, filter = NULL)dataset_vignette(dataset, display_vars = NULL, filter = NULL)
dataset |
Dataset to output in the vignette
|
display_vars |
Variables selected to demonstrate the outcome of the derivation If
|
filter |
Filter condition The specified condition is applied to the dataset before it is displayed.
|
A HTML table
Wrapper around lifecycle::deprecate_soft() that messages users about
deprecated features and functions instead of warning.
deprecate_inform( when, what, with = NULL, details = NULL, id = NULL, env = rlang::caller_env(), user_env = rlang::caller_env(2) )deprecate_inform( when, what, with = NULL, details = NULL, id = NULL, env = rlang::caller_env(), user_env = rlang::caller_env(2) )
when |
A string giving the version when the behaviour was deprecated. |
what |
A string describing what is deprecated:
You can optionally supply the namespace: |
with |
An optional string giving a recommended replacement for the
deprecated behaviour. This takes the same form as |
details |
In most cases the deprecation message can be
automatically generated from
|
id |
The id of the deprecation. A warning is issued only once for each
|
env, user_env
|
Pair of environments that define where These are only needed if you're calling |
NULL, invisibly.
# A Phase 1 deprecated function with custom bulleted list: deprecate_inform( when = "1.0.0", what = "foo()", details = c( x = "This message will turn into a warning with release of x.y.z", i = "See admiral's deprecation guidance: https://pharmaverse.github.io/admiraldev/dev/articles/programming_strategy.html#deprecation" ) )# A Phase 1 deprecated function with custom bulleted list: deprecate_inform( when = "1.0.0", what = "foo()", details = c( x = "This message will turn into a warning with release of x.y.z", i = "See admiral's deprecation guidance: https://pharmaverse.github.io/admiraldev/dev/articles/programming_strategy.html#deprecation" ) )
Wrap a string in double quotes, e.g., for displaying character values in messages.
dquote(x)dquote(x)
x |
A character vector
|
If the input is NULL, the text "NULL" is returned. Otherwise, the
input in double quotes is returned.
Helpers for working with Quotes and Quoting:
backquote(),
squote()
This function is deprecated, please use cli functionality instead.
enumerate(x, quote_fun = backquote, conjunction = "and")enumerate(x, quote_fun = backquote, conjunction = "and")
x |
A vector or list
|
quote_fun |
Quoting function, defaults to
|
conjunction |
Character to be used in the message, defaults to
|
A character vector
Other deprecated:
%or%,
arg_name(),
friendly_type_of(),
valid_time_units(),
what_is_it()
Uses diffdf::diffdf() to compares 2 datasets for any differences. This function can be
thought of as an R-equivalent of SAS proc compare and a useful tool for unit testing as well.
expect_dfs_equal(base, compare, keys, ...)expect_dfs_equal(base, compare, keys, ...)
base |
Input dataset
|
compare |
Comparison dataset
|
keys |
|
... |
Additional arguments passed onto
|
An error if base and compare do not match or NULL invisibly if they do
library(dplyr, warn.conflicts = FALSE) tbl1 <- tribble( ~USUBJID, ~AGE, ~SEX, "1001", 18, "M", "1002", 19, "F", "1003", 20, "M", "1004", 18, "F" ) tbl2 <- tribble( ~USUBJID, ~AGE, ~SEX, "1001", 18, "M", "1002", 18.9, "F", "1003", 20, NA ) try(expect_dfs_equal(tbl1, tbl2, keys = "USUBJID")) tlb3 <- tribble( ~USUBJID, ~AGE, ~SEX, "1004", 18, "F", "1003", 20, "M", "1002", 19, "F", "1001", 18, "M", ) # Note the sorting order of the keys is not required expect_dfs_equal(tbl1, tlb3, keys = "USUBJID")library(dplyr, warn.conflicts = FALSE) tbl1 <- tribble( ~USUBJID, ~AGE, ~SEX, "1001", 18, "M", "1002", 19, "F", "1003", 20, "M", "1004", 18, "F" ) tbl2 <- tribble( ~USUBJID, ~AGE, ~SEX, "1001", 18, "M", "1002", 18.9, "F", "1003", 20, NA ) try(expect_dfs_equal(tbl1, tbl2, keys = "USUBJID")) tlb3 <- tribble( ~USUBJID, ~AGE, ~SEX, "1004", 18, "F", "1003", 20, "M", "1002", 19, "F", "1001", 18, "M", ) # Note the sorting order of the keys is not required expect_dfs_equal(tbl1, tlb3, keys = "USUBJID")
Concatenate One or More Expressions
expr_c(...)expr_c(...)
... |
One or more expressions or list of expressions
|
A list of expressions
Helpers for working with Quosures:
add_suffix_to_vars(),
replace_symbol_in_expr(),
replace_values_by_names()
Extract All Symbols from a List of Expressions
extract_vars(x, side = "lhs")extract_vars(x, side = "lhs")
x |
An
|
side |
One of
|
A list of expressions
Developer Utility Functions:
%notin%,
contains_vars(),
convert_dtm_to_dtc(),
filter_if(),
vars2chr()
library(rlang) extract_vars(exprs(PARAMCD, (BASE - AVAL) / BASE + 100)) extract_vars(AVAL ~ ARMCD + AGEGR1) extract_vars(AVAL ~ ARMCD + AGEGR1, side = "rhs")library(rlang) extract_vars(exprs(PARAMCD, (BASE - AVAL) / BASE + 100)) extract_vars(AVAL ~ ARMCD + AGEGR1) extract_vars(AVAL ~ ARMCD + AGEGR1, side = "rhs")
Filters the input dataset if the provided expression is not NULL
filter_if(dataset, filter)filter_if(dataset, filter)
dataset |
Input dataset
|
filter |
A filter condition. Must be an expression.
|
A data.frame containing all rows in dataset matching filter or
just dataset if filter is NULL
Developer Utility Functions:
%notin%,
contains_vars(),
convert_dtm_to_dtc(),
extract_vars(),
vars2chr()
This function is deprecated, please use cli functionality instead.
friendly_type_of(x, value = TRUE, length = FALSE)friendly_type_of(x, value = TRUE, length = FALSE)
x |
Any R object.
|
value |
Whether to describe the value of
|
length |
Whether to mention the length of vectors and lists.
|
This helper function aids us in forming user-friendly messages that gets
called through what_is_it(), which is often used in the assertion functions
to identify what object-type the user passed through an argument instead of
an expected-type.
A string describing the type. Starts with an indefinite article, e.g. "an integer vector".
Other deprecated:
%or%,
arg_name(),
enumerate(),
valid_time_units(),
what_is_it()
Get Constant Variables
get_constant_vars(dataset, by_vars, ignore_vars = NULL)get_constant_vars(dataset, by_vars, ignore_vars = NULL)
dataset |
A data frame.
|
by_vars |
By variables The groups defined by the by variables are considered separately. I.e., if a variable is constant within each by group, it is returned.
|
ignore_vars |
Variables to ignore The specified variables are not considered, i.e., they are not returned even if they are constant (unless they are included in the by variables).
|
Variable vector.
Brings something to you!?!:
get_dataset(),
get_duplicates(),
get_source_vars()
admiraldev_environment environmentRetrieve a Dataset from the admiraldev_environment environment
get_dataset(name)get_dataset(name)
name |
The name of the dataset to retrieve
|
Sometimes, developers may want to provide information to users which does not fit into a warning or error message. For example, if the input dataset of a function contains unexpected records, these can be stored in a separate dataset, which users can access to investigate the issue.
To achieve this, R has a data structure known as an 'environment'. These environment objects are created at build time, but can be populated with values after the package has been loaded and update those values over the course of an R session.
As so, the establishment of admiraldev_environment allows us to create dynamic data/objects
based on user-inputs that need modification. The purpose of get_dataset is to
retrieve the datasets contained inside admiraldev_environment.
Currently we only support two datasets inside our admiraldev_environment object:
one_to_many
many_to_one
A data.frame
Brings something to you!?!:
get_constant_vars(),
get_duplicates(),
get_source_vars()
Get Duplicates From a Vector
get_duplicates(x)get_duplicates(x)
x |
An atomic vector
|
A vector of the same type as x contain duplicate values
Brings something to you!?!:
get_constant_vars(),
get_dataset(),
get_source_vars()
get_duplicates(1:10) get_duplicates(c("a", "a", "b", "c", "d", "d"))get_duplicates(1:10) get_duplicates(c("a", "a", "b", "c", "d", "d"))
Get a New Temporary Variable Name for a Dataset
get_new_tmp_var(dataset, prefix = "tmp_var")get_new_tmp_var(dataset, prefix = "tmp_var")
dataset |
The input dataset
|
prefix |
The prefix of the new temporary variable name to create
|
The function returns a new unique temporary variable name to be used inside
dataset. The temporary variable names have the structure prefix_n where
n is an integer, e.g. tmp_var_1. If there is already a variable inside
datset with a given prefix then the suffix is increased by 1, e.g. if
tmp_var_1 already exists then get_new_tmp_var() will return tmp_var_2.
The name of a new temporary variable as a symbol
library(dplyr, warn.conflicts = FALSE) dm <- tribble( ~DOMAIN, ~STUDYID, ~USUBJID, "DM", "STUDY X", "01-701-1015", "DM", "STUDY X", "01-701-1016", ) tmp_var <- get_new_tmp_var(dm) mutate(dm, !!tmp_var := NA)library(dplyr, warn.conflicts = FALSE) dm <- tribble( ~DOMAIN, ~STUDYID, ~USUBJID, "DM", "STUDY X", "01-701-1015", "DM", "STUDY X", "01-701-1016", ) tmp_var <- get_new_tmp_var(dm) mutate(dm, !!tmp_var := NA)
Get Source Variables from a List of Expressions
get_source_vars(expressions)get_source_vars(expressions)
expressions |
A list of expressions
|
A list of expressions
Brings something to you!?!:
get_constant_vars(),
get_dataset(),
get_duplicates()
Checks if the argument equals the auto keyword
is_auto(arg)is_auto(arg)
arg |
argument to check
|
TRUE if the argument equals the auto keyword, i.e., it is an
expression of a symbol named auto.
Identifies type of Object with return of TRUE/FALSE:
is_order_vars(),
is_valid_dtc()
Check if inputs are created using exprs() or calls involving desc()
is_order_vars(arg)is_order_vars(arg)
arg |
An R object
|
FALSE if the argument is not a list of order vars
Identifies type of Object with return of TRUE/FALSE:
is_auto(),
is_valid_dtc()
Is this string a valid DTC?
is_valid_dtc(arg, check_dtc = FALSE)is_valid_dtc(arg, check_dtc = FALSE)
arg |
The string to check.
|
check_dtc |
If
|
If check_dtc is FALSE, the function only checks if the format of the
string is valid, i.e. it will not check if the actual date/datetime is real, so
calls such as is_valid_dtc("2020-13", check_dtc = FALSE) and
is_valid_dtc("2020-12-01T25:00:00", check_dtc = FALSE) will returnTRUE.
TRUE if the argument is a valid --DTC string, FALSE otherwise
Identifies type of Object with return of TRUE/FALSE:
is_auto(),
is_order_vars()
## Format is valid, date/datetimes are real is_valid_dtc("2020-02") is_valid_dtc("2020-02-28") is_valid_dtc("2020-02-28T14:43") # Format is valid, date/datetimes are not real but this is not checked is_valid_dtc("2020-02-31") is_valid_dtc("2020-02-28T25:00:00") # Format is valid, date/datetimes are not real and this is checked is_valid_dtc("2020-02-31", check_dtc = TRUE) is_valid_dtc("2020-02-28T25:00:00", check_dtc = TRUE)## Format is valid, date/datetimes are real is_valid_dtc("2020-02") is_valid_dtc("2020-02-28") is_valid_dtc("2020-02-28T14:43") # Format is valid, date/datetimes are not real but this is not checked is_valid_dtc("2020-02-31") is_valid_dtc("2020-02-28T25:00:00") # Format is valid, date/datetimes are not real and this is checked is_valid_dtc("2020-02-31", check_dtc = TRUE) is_valid_dtc("2020-02-28T25:00:00", check_dtc = TRUE)
The function parses the code and returns a list of expressions and source references.
parse_code(code)parse_code(code)
code |
The code to parse
|
A list of expressions and source references
Each item of the list is a list with the following elements:
expr: The expression
srcref: The source reference
eval: A logical indicating whether the expression should be evaluated,
i.e., it is not a comment or an empty line.
parse_code("1+1\n2*2") parse_code(c("# sum:", "sum(\n 1, #first\n 2\n)"))parse_code("1+1\n2*2") parse_code(c("# sum:", "sum(\n 1, #first\n 2\n)"))
set_values_to ArgumentThe function creates the variables specified by the set_values_to argument,
catches errors, provides user friendly error messages, and optionally checks
the type of the created variables.
process_set_values_to(dataset, set_values_to = NULL, expected_types = NULL)process_set_values_to(dataset, set_values_to = NULL, expected_types = NULL)
dataset |
Input dataset
|
set_values_to |
Variables to set A named list returned by
|
expected_types |
If the argument is specified, the specified variables are checked whether
the specified type matches the type of the variables created by
|
The input dataset with the variables specified by set_values_to
added/updated
library(dplyr) data <- tribble( ~AVAL, 20 ) try( process_set_values_to( data, set_values_to = exprs( PARAMCD = BMI ) ) ) try( process_set_values_to( data, set_values_to = exprs( PARAMCD = 42 ), expected_types = c(PARAMCD = "character") ) )library(dplyr) data <- tribble( ~AVAL, 20 ) try( process_set_values_to( data, set_values_to = exprs( PARAMCD = BMI ) ) ) try( process_set_values_to( data, set_values_to = exprs( PARAMCD = 42 ), expected_types = c(PARAMCD = "character") ) )
This roclet extends the standard rd roclet by allowing
to add permitted values and default values to the @param tag and
to add a caption and a description to examples.
rdx_roclet()rdx_roclet()
The following tags are supported:
@permitted: Permitted values for the argument. Permitted value description
which are used for several arguments/functions can be stored in
inst/roxygen/rdx_meta.R. For example:
list(
rdx_permitted_values = list(
mode = "`\"first\"`, `\"last\"`",
msg_type = "`\"none\"`, `\"message\"`, `\"warning\"`, `\"error\"`"
)
)
The reference to the permitted values is done by specifying the name of
the list element in square brackets, e.g., @permitted [mode].
@default: Default value for the argument. By default the default value
from the function formals is displayed. This can be overwritten by using
the @default tag.
@examplesx: This tag can be used to mark the beginning of the examples
section but doesn't affect the output, i.e., it can be omitted.
@caption: Caption for the example. The caption is displayed as a subsection
in the examples section. The caption can be followed by an arbitrary number
of @info and @code tags.
@info: Description of the example.
@code: Code of the example.
By default, any warning or error issued by the example code causes the
building of the documentation to fail. If this is expected, the condition
can be added to the expected_cnds option of the @code tag. E.g.,
@code [expected_cnds = "warning"]
To use the roclet call roxygen2::roxygenise(roclets = "admiral::rdx_roclet") or add to the DESCRIPTION file:
Roxygen: list(markdown = TRUE, roclets = c("collate", "namespace", "admiraldev::rdx_roclet"))
For more information on roxygen2 roclets see the Extending roxygen2.
The id char_scalar used for the @permitted tag is defined in
man/roxygen/rdx_meta.R.
See demo_fun() for a rendered version of the Rd code generated in the
example.
roxygen2::roc_proc_text(
rdx_roclet(),
c(
"#' A Demo Function",
"#'",
"#' This function is used to demonstrate the custom tags of the `rdx_roclet()`.",
"#'",
"#' @param x An argument",
"#' @param number A number",
"#' @permitted A number",
"#' @param letter A letter",
"#' @permitted [char_scalar]",
"#' @default The first letter of the alphabet",
"#' @examplesx",
"#' @caption A simple example",
"#' @info This is a simple example showing the default behaviour.",
"#' @code demo_fun(1)",
"#' @caption An example with a different letter",
"#' @info This example shows that the `letter` argument doesn't",
"#' affect the output. ",
"#' @code demo_fun(1, letter = \"b\")",
"demo_fun <- function(x, number = 1, letter = \"a\") 42"
))
#> $demo_fun.Rd
#> % Generated by roxygen2: do not edit by hand
#> % Please edit documentation in ./<text>
#> \name{demo_fun}
#> \alias{demo_fun}
#> \title{A Demo Function}
#> \usage{
#> demo_fun(x, number = 1, letter = "a")
#> }
#> \arguments{
#> \item{x}{An argument
#>
#> \describe{
#> \item{Default value}{none}
#> }}
#>
#> \item{number}{A number
#>
#> \describe{
#> \item{Permitted values}{A number}
#> \item{Default value}{\code{1}}
#> }}
#>
#> \item{letter}{A letter
#>
#> \describe{
#> \item{Permitted values}{a character scalar, i.e., a character vector of length one}
#> \item{Default value}{The first letter of the alphabet}
#> }}
#> }
#> \description{
#> This function is used to demonstrate the custom tags of the \code{rdx_roclet()}.
#> }
#> \section{Examples}{
#> \subsection{A simple example}{
#>
#> This is a simple example showing the default behaviour.
#>
#> \if{html}{\out{<div class="sourceCode r">}}\preformatted{demo_fun(1)
#> #> [1] 42}\if{html}{\out{</div>}}}
#> \subsection{An example with a different letter}{
#>
#> This example shows that the \code{letter} argument doesn't
#> affect the output.
#>
#> \if{html}{\out{<div class="sourceCode r">}}\preformatted{demo_fun(1, letter = "b")
#> #> [1] 42}\if{html}{\out{</div>}}}}
#>
#> Remove All Temporary Variables Created Within the Current Function Environment
remove_tmp_vars(dataset)remove_tmp_vars(dataset)
dataset |
The input dataset
|
The input dataset with temporary variables removed
library(dplyr, warn.conflicts = FALSE) dm <- tribble( ~DOMAIN, ~STUDYID, ~USUBJID, "DM", "STUDY X", "01-701-1015", "DM", "STUDY X", "01-701-1016", ) dm <- select(dm, USUBJID) tmp_var <- get_new_tmp_var(dm) dm <- mutate(dm, !!tmp_var := NA) ## This function creates two new temporary variables which are removed when calling ## `remove_tmp_vars()`. Note that any temporary variable created outside this ## function is **not** removed do_something <- function(dataset) { tmp_var_1 <- get_new_tmp_var(dm) tmp_var_2 <- get_new_tmp_var(dm) dm %>% mutate(!!tmp_var_1 := NA, !!tmp_var_2 := NA) %>% print() %>% remove_tmp_vars() } do_something(dm)library(dplyr, warn.conflicts = FALSE) dm <- tribble( ~DOMAIN, ~STUDYID, ~USUBJID, "DM", "STUDY X", "01-701-1015", "DM", "STUDY X", "01-701-1016", ) dm <- select(dm, USUBJID) tmp_var <- get_new_tmp_var(dm) dm <- mutate(dm, !!tmp_var := NA) ## This function creates two new temporary variables which are removed when calling ## `remove_tmp_vars()`. Note that any temporary variable created outside this ## function is **not** removed do_something <- function(dataset) { tmp_var_1 <- get_new_tmp_var(dm) tmp_var_2 <- get_new_tmp_var(dm) dm %>% mutate(!!tmp_var_1 := NA, !!tmp_var_2 := NA) %>% print() %>% remove_tmp_vars() } do_something(dm)
Replace symbols in an expression
replace_symbol_in_expr(expression, target, replace)replace_symbol_in_expr(expression, target, replace)
expression |
Expression
|
target |
Target symbol
|
replace |
Replacing symbol
|
The expression where every occurrence of the symbol target is
replaced by replace
Stefan Bundfuss
Helpers for working with Quosures:
add_suffix_to_vars(),
expr_c(),
replace_values_by_names()
library(rlang) replace_symbol_in_expr(expr(AVAL), target = AVAL, replace = AVAL.join) replace_symbol_in_expr(expr(AVALC), target = AVAL, replace = AVAL.join) replace_symbol_in_expr(expr(desc(AVAL)), target = AVAL, replace = AVAL.join) replace_symbol_in_expr(expr(if_else(AVAL > 0, AVAL, NA)), AVAL, AVAL.join)library(rlang) replace_symbol_in_expr(expr(AVAL), target = AVAL, replace = AVAL.join) replace_symbol_in_expr(expr(AVALC), target = AVAL, replace = AVAL.join) replace_symbol_in_expr(expr(desc(AVAL)), target = AVAL, replace = AVAL.join) replace_symbol_in_expr(expr(if_else(AVAL > 0, AVAL, NA)), AVAL, AVAL.join)
Replace Expression Value with Name
replace_values_by_names(expressions)replace_values_by_names(expressions)
expressions |
A list of expressions
|
A list of expressions
Helpers for working with Quosures:
add_suffix_to_vars(),
expr_c(),
replace_symbol_in_expr()
library(rlang) replace_values_by_names(exprs(AVAL, ADT = convert_dtc_to_dt(EXSTDTC)))library(rlang) replace_values_by_names(exprs(AVAL, ADT = convert_dtc_to_dt(EXSTDTC)))
This function provides standardized documentation text about the handling
of NA values in sorting variables.
roxygen_order_na_handling()roxygen_order_na_handling()
The benefits of having a programmatic way to write documentation is that if any changes need to be made, modifying this function scales across the codebase, can be tested, and is less prone to user-error such as typos or grammar mistakes.
A character string with the standardized documentation text about NA handling in sort order
Other documentation:
roxygen_param_by_vars(),
roxygen_param_dataset(),
roxygen_save_memory()
roxygen_order_na_handling()roxygen_order_na_handling()
by_vars ArgumentThis function provides standardized documentation text for the by_vars
argument used in {admiral} functions.
roxygen_param_by_vars(rename = FALSE)roxygen_param_by_vars(rename = FALSE)
rename |
Should the renaming feature be documented? If
|
The benefits of having a programmatic way to write documentation is that if any changes need to be made, modifying this function scales across the codebase, can be tested, and is less prone to user-error such as typos or grammar mistakes.
A character string with the standardized documentation text for
the by_vars argument
Other documentation:
roxygen_order_na_handling(),
roxygen_param_dataset(),
roxygen_save_memory()
roxygen_param_by_vars() roxygen_param_by_vars(rename = TRUE)roxygen_param_by_vars() roxygen_param_by_vars(rename = TRUE)
dataset ArgumentThis function provides standardized documentation text for the dataset
argument used in {admiral} functions.
roxygen_param_dataset(expected_vars = NULL)roxygen_param_dataset(expected_vars = NULL)
expected_vars |
Variables expected in the dataset A character vector of variable names expected to be present in the
|
The benefits of having a programmatic way to write documentation is that if any changes need to be made, modifying this function scales across the codebase, can be tested, and is less prone to user-error such as typos or grammar mistakes.
A character string with the standardized documentation text for
the dataset argument
Other documentation:
roxygen_order_na_handling(),
roxygen_param_by_vars(),
roxygen_save_memory()
roxygen_param_dataset() roxygen_param_dataset(expected_vars = c("by_vars"))roxygen_param_dataset() roxygen_param_dataset(expected_vars = c("by_vars"))
This function provides a standardized note about memory consumption for functions that create large temporary datasets.
roxygen_save_memory()roxygen_save_memory()
The benefits of having a programmatic way to write documentation is that if any changes need to be made, modifying this function scales across the codebase, can be tested, and is less prone to user-error such as typos or grammar mistakes.
A character string with the standardized note about memory usage
Other documentation:
roxygen_order_na_handling(),
roxygen_param_by_vars(),
roxygen_param_dataset()
roxygen_save_memory()roxygen_save_memory()
Wrap a String in Single Quotes
squote(x)squote(x)
x |
A
|
A character vector
Helpers for working with Quotes and Quoting:
backquote(),
dquote()
Suppress certain warnings issued by an expression.
suppress_warning(expr, regexpr)suppress_warning(expr, regexpr)
expr |
Expression to be executed
|
regexpr |
Regular expression matching warnings to suppress
|
All warnings which are issued by the expression and match the regular expression are suppressed.
Return value of the expression
Function that provide users with custom warnings:
warn_if_incomplete_dtc(),
warn_if_inconsistent_list(),
warn_if_invalid_dtc(),
warn_if_vars_exist()
This function is deprecated. Please get in touch if you are using this function!
Contains the acceptable character vector of valid time units
valid_time_units()valid_time_units()
A character vector of valid time units
Other deprecated:
%or%,
arg_name(),
enumerate(),
friendly_type_of(),
what_is_it()
Turn a List of Expressions into a Character Vector
vars2chr(expressions)vars2chr(expressions)
expressions |
A
|
A character vector
Developer Utility Functions:
%notin%,
contains_vars(),
convert_dtm_to_dtc(),
extract_vars(),
filter_if()
library(dplyr, warn.conflicts = FALSE) library(rlang) vars2chr(exprs(USUBJID, AVAL))library(dplyr, warn.conflicts = FALSE) library(rlang) vars2chr(exprs(USUBJID, AVAL))
Warn if incomplete dtc
warn_if_incomplete_dtc(dtc, n)warn_if_incomplete_dtc(dtc, n)
dtc |
A
|
n |
A non-negative integer
|
A warning if dtc contains any partial dates
Function that provide users with custom warnings:
suppress_warning(),
warn_if_inconsistent_list(),
warn_if_invalid_dtc(),
warn_if_vars_exist()
Checks if two list inputs have the same names and same number of elements and issues a warning otherwise.
warn_if_inconsistent_list(base, compare, list_name, i = 2)warn_if_inconsistent_list(base, compare, list_name, i = 2)
base |
A named list
|
compare |
A named list
|
list_name |
A string the name of the list
|
i |
the index id to compare the 2 lists
|
a warning if the 2 lists have different names or length
Function that provide users with custom warnings:
suppress_warning(),
warn_if_incomplete_dtc(),
warn_if_invalid_dtc(),
warn_if_vars_exist()
library(dplyr, warn.conflicts = FALSE) library(rlang) # no warning warn_if_inconsistent_list( base = exprs(DTHDOM = "DM", DTHSEQ = DMSEQ), compare = exprs(DTHDOM = "DM", DTHSEQ = DMSEQ), list_name = "Test" ) # warning warn_if_inconsistent_list( base = exprs(DTHDOM = "DM", DTHSEQ = DMSEQ, DTHVAR = "text"), compare = exprs(DTHDOM = "DM", DTHSEQ = DMSEQ), list_name = "Test" )library(dplyr, warn.conflicts = FALSE) library(rlang) # no warning warn_if_inconsistent_list( base = exprs(DTHDOM = "DM", DTHSEQ = DMSEQ), compare = exprs(DTHDOM = "DM", DTHSEQ = DMSEQ), list_name = "Test" ) # warning warn_if_inconsistent_list( base = exprs(DTHDOM = "DM", DTHSEQ = DMSEQ, DTHVAR = "text"), compare = exprs(DTHDOM = "DM", DTHSEQ = DMSEQ), list_name = "Test" )
Warn if the vector contains unknown datetime format such as "2003-12-15T-:15:18", "2003-12-15T13:-:19","–12-15","—–T07:15"
warn_if_invalid_dtc(dtc, is_valid = is_valid_dtc(dtc))warn_if_invalid_dtc(dtc, is_valid = is_valid_dtc(dtc))
dtc |
a character vector containing the dates
|
is_valid |
a logical vector indicating whether elements in
|
No return value, called for side effects
Function that provide users with custom warnings:
suppress_warning(),
warn_if_incomplete_dtc(),
warn_if_inconsistent_list(),
warn_if_vars_exist()
## No warning as `dtc` is a valid date format warn_if_invalid_dtc(dtc = "2021-04-06") ## Issues a warning warn_if_invalid_dtc(dtc = "2021-04-06T-:30:30")## No warning as `dtc` is a valid date format warn_if_invalid_dtc(dtc = "2021-04-06") ## Issues a warning warn_if_invalid_dtc(dtc = "2021-04-06T-:30:30")
Warn if a variable already exists inside a dataset
warn_if_vars_exist(dataset, vars)warn_if_vars_exist(dataset, vars)
dataset |
A
|
vars |
|
No return value, called for side effects
Function that provide users with custom warnings:
suppress_warning(),
warn_if_incomplete_dtc(),
warn_if_inconsistent_list(),
warn_if_invalid_dtc()
library(dplyr, warn.conflicts = FALSE) dm <- tribble( ~USUBJID, ~ARM, "01-701-1015", "Placebo", "01-701-1016", "Placebo", ) ## No warning as `AAGE` doesn't exist in `dm` warn_if_vars_exist(dm, "AAGE") ## Issues a warning warn_if_vars_exist(dm, "ARM")library(dplyr, warn.conflicts = FALSE) dm <- tribble( ~USUBJID, ~ARM, "01-701-1015", "Placebo", "01-701-1016", "Placebo", ) ## No warning as `AAGE` doesn't exist in `dm` warn_if_vars_exist(dm, "AAGE") ## Issues a warning warn_if_vars_exist(dm, "ARM")
This function is deprecated, please use cli functionality instead.
what_is_it(x)what_is_it(x)
x |
Any R object
|
A character description of the type of x
Other deprecated:
%or%,
arg_name(),
enumerate(),
friendly_type_of(),
valid_time_units()