| where_to_get_args {matsindf} | R Documentation |
Decide where to get each argument to FUN
Description
The precedence rules for where to obtain values for the FUN argument to
matsindf_apply() are codified here.
The rules are:
Precedence order:
...,.dat, defaults arguments toFUN(highest priority to lowest priority).If an element of
...is a character string of length1, the element of...provides a mapping between an item in.dat(with same name as the value of the character string of length1) to an argument ofFUN(with the same name as the name of the character string of length1).If the value of the character string of length
1is not a name in.dat, the default arguments toFUNare checked in this order.If the name of a default argument to
FUNis the same as the value of the string of length1argument in..., a mapping occurs.If a mapping is not possible, the default arg to
FUNis used directly.
Usage
where_to_get_args(.dat = NULL, FUN, ...)
Arguments
.dat |
The |
FUN |
The |
... |
The |
Value
A named list wherein the names are the argument names to FUN.
Values are character vectors with 2 elements.
The first element is named source and provides
the argument to matsindf_apply() from which the named argument should be found,
one of ".dat", "FUN", or "...".
The second element is named arg_name and provides
the variable name or argument name in the source that contains the input data
for the argument to FUN.
Examples
example_fun <- function(a = 1, b) {
list(c = a + b, d = a - b)
}
# b is not available anywhere, likely causing an error later
matsindf:::where_to_get_args(FUN = example_fun)
# b is now available in ...
matsindf:::where_to_get_args(FUN = example_fun, b = 2)
# b is now available in .dat
matsindf:::where_to_get_args(list(b = 2), FUN = example_fun)
# b now comes from ..., because ... takes precedence over .dat
matsindf:::where_to_get_args(list(b = 2), FUN = example_fun, b = 3)
# Mapping from c in .dat to b in FUN
matsindf:::where_to_get_args(list(c = 2),
FUN = example_fun, b = "c")
# Redirect from an arg in ... to a different default to FUN
matsindf:::where_to_get_args(FUN = example_fun, b = "a")
# b is found in FUN, not in .dat, because the mapping (b = "a")
# is not available in .dat
matsindf:::where_to_get_args(list(b = 2), FUN = example_fun, b = "a")