I have an R function, fun
, with many parameters. I need to run fun
several times, varying the parameters y
and z
but keeping the others the same. Rather than wrap every call of fun
in a loop, I made y
and z
both NA
by default; the function checks if is.na(y)
or is.na(z)
and, if so, calls itself recursively with each desired value of y
and z
in turn, like this:
fun <- function(a=defaultA, b=defaultB, ..., y = NA, z=NA) {
if (is.na(y)) {
for (yValue in 1:maxY) {
fun(a, b, ..., y = yvalue, z = z)
}
return()
}
if (is.na(z)) {
for (zValue in 1:maxZ) {
fun(a, b, ..., y, z=zValue)
}
return()
}
stopifnot(!is.na(y) && !is.na(z))
# Part of fun that does actual work follows.
...
}
The recursive fun
calls are actually inconveniently long, a few hundred characters each, because they need to repeat every parameter given to fun
. Is there any shortcut by which I can call fun
recursively with all parameters but one preserved, or is there another, better way to accomplish what I'm trying to do?
For the curious: my actual fun
produces a graph with data sets and options specified in the parameters, and y
and z
are Booleans that determine whether to graph specific outliers. This lets me produce with- and without-outliers versions of the graphs without duplicating the function calls or wrapping them in for-loops as for (y in c(TRUE, FALSE)) {for (z in c(TRUE, FALSE)) {fun(...)}}
.