This function maps the arguments of a target function
to the desired names. Users will use a named list name_match
to
standardize the argument names, at least x and y, to the target function.
This function is particularly useful to parallelize functions for spatial
data outside sf
and terra
packages that do not have arguments
named x and/or y. par_*
functions could detect such functions by
wrapping nonstandardized functions to parallelize the computation.
Note
arg_map
should be defined carefully according to the characteristics
of fun
. After mapping x
and y
, the resultant function will fail
if there remain arguments without default. It is recommended to map all
the arguments in fun
to avoid any side effects.
Examples
cov_map <- arg_mapping <- c(x = "a", y = "b", z = "c", w = "d")
# Example original function
f1 <- function(a, b, c, d) {
return(a + b + c + d)
}
# Mapping of new argument names to original argument names
arg_mapping <- c(x = "a", y = "b", z = "c", w = "d")
f2 <- par_convert_f(f1, arg_mapping)
# demonstrate f2 with the mapped arguments
f2(x = 1, y = 2, z = -1, w = 10)
#> [1] 12