Checks whether an argument is a formula.
Usage
arg_formula(
x,
one_sided = NULL,
.arg = rlang::caller_arg(x),
.msg = NULL,
.call
)Arguments
- x
the argument to be checked
- one_sided
NULLorlogical; ifTRUE, checks thatxis aformulawith only one side (the right side); ifFALSE, checks thatxis aformulawith both sides; ifNULL(the default), checks only thatxis aformula.- .arg
the name of the argument supplied to
xto appear in error messages. The default is to extract the argument's name usingrlang::caller_arg(). Ignored if.msgis supplied.- .msg
an optional alternative message to display if an error is thrown instead of the default message.
- .call
the execution environment of a currently running function, e.g.
.call = rlang::current_env(). The corresponding function call is retrieved and mentioned in error messages as the source of the error. Passed toerr(). Set toNULLto omit call information. The default is to search along the call stack for the first user-facing function in another package, if any.
Examples
form1 <- ~a + b
form2 <- y ~ a + b
not_form <- 1:3
try(arg_formula(form1)) # No error
try(arg_formula(form2)) # No error
try(arg_formula(not_form)) # Error: not a formula
#> Error : `not_form` must be a formula.
try(arg_formula(form1,
one_sided = TRUE)) # No error
try(arg_formula(form2,
one_sided = TRUE)) # Error, not one-sided
#> Error : `form2` must be a one-sided formula.
try(arg_formula(form1,
one_sided = FALSE)) # Error, only one-sided
#> Error : `form1` must be a two-sided formula.
try(arg_formula(form2,
one_sided = FALSE)) # No error