Safely call a function using ellipsis
Details
This function is a wrapper function intended to help
pass ellipsis arguments ... from a parent function
to an external function in a safe way. It will only
include arguments from ... that are recognized by
the external function.
The logic is described as follows:
When the external function
FUNargumentsformals()include ellipsis..., then the ellipsis...will be passed as-is without change. In this way, any arguments inside the original ellipsis...will either match arguments inFUN, or will be ignored in that function ellipsis....When the external function
FUNargumentsformals()do not include ellipsis..., then named arguments in...are passed toFUNonly when the arguments names are recognized byFUN.
Note that arguments therefore must be named.
See also
Other jam practical functions:
breakDensity(),
checkLightMode(),
check_pkg_installed(),
colNum2excelName(),
color_dither(),
exp2signed(),
getAxisLabel(),
isFALSEV(),
isTRUEV(),
jargs(),
kable_coloring(),
lldf(),
log2signed(),
middle(),
minorLogTicks(),
newestFile(),
printDebug(),
reload_rmarkdown_cache(),
renameColumn(),
rmInfinite(),
rmNA(),
rmNAs(),
rmNULL(),
setPrompt()
Examples
new_mean <- function(x, trim=0, na.rm=FALSE) {
mean(x, trim=trim, na.rm=na.rm)
}
x <- c(1, 3, 5, NA);
new_mean(x, na.rm=TRUE);
#> [1] 3
# throws an error as expected (below)
tryCatch({
new_mean(x, na.rm=TRUE, color="red")
}, error=function(e){
print("Error is expected, shown below:");
print(e)
})
#> [1] "Error is expected, shown below:"
#> <simpleError in new_mean(x, na.rm = TRUE, color = "red"): unused argument (color = "red")>
call_fn_ellipsis(new_mean, x=x, na.rm=TRUE, color="red")
#> [1] 3