Learn R Programming

rlang (version 1.1.3)

caller_arg: Find the caller argument for error messages

Description

caller_arg() is a variant of substitute() or ensym() for arguments that reference other arguments. Unlike substitute() which returns an expression, caller_arg() formats the expression as a single line string which can be included in error messages.

  • When included in an error message, the resulting label should generally be formatted as argument, for instance using the .arg in the cli package.

  • Use @inheritParams rlang::args_error_context to document an arg or error_arg argument that takes error_arg() as default.

Arguments

arg

An argument name in the current function.

Examples

Run this code
arg_checker <- function(x, arg = caller_arg(x), call = caller_env()) {
  cli::cli_abort("{.arg {arg}} must be a thingy.", arg = arg, call = call)
}

my_function <- function(my_arg) {
  arg_checker(my_arg)
}

try(my_function(NULL))

Run the code above in your browser using DataLab