In R, the idiomatic way to call another function without evaluating the parameters you give it is apparently as follows:
Call <- match.call(expand.dots = TRUE)
# Modify parameters here as needed and set unneeded ones to NULL.
Call[[1L]] <- as.name("name.of.function.to.be.called.here")
eval.parent(Call)
However, when I put a namespaced name (e.g. utils::write.csv) in the as.name() call, I get an error:
“could not find function “utils::write.csv”
What is the proper way of using this R idiom to call a namespaced function?
Here is a solution using
do.call(), which both constructs and evaluates the function call.Like the approach you started with, this one uses the fact that R calls are lists in which: (a) the first element is the name of a function; and (b) all following elements are arguments to that function.
EDIT: FWIW, here’s an example from
plot.formulain base R, which uses a construct similar to the one above:The function uses the
do.call()construct later on. Going a bit deeper into the weeds, my reading is that in the snippet shown here, it instead uses several steps mostly because of the need to addna.action=NULLto the list of arguments.In any case, it looks like the
do.call()options is as close to canonical as could be desired.