Can't find a question similar to what I am trying to do. I have a workhorse function that can take any of a class of custom-made sub-functions, with their respective arguments, and do certain processing on it, like the following toy examples:
func1 <- function(a, b, c) {a + b * c}
func2 <- function(x, y, z, bob, bleb) {(x / y)^bleb * z/bob}
workhorse <- function(m, n, o, FUN, ...) {
result <- FUN(x, y, ...)
[process result with m, n, and o]
}
By itself, this works perfectly well as intended, as in
foo <- workhorse(m, n, o, func1, a, b, c)
foo2 <- workhorse(m, n, o, func2, x, y, z, bob, bleb)
I am trying to create a wrapper for workhorse (meta_workhorse) that will accept a list of functions (by quoted name) to run through workhorse and combine the results, something like below.
FUN_list <- c('func1', 'func2')
ARGS_list <- list(c(a, b, c), c(x, y, z, bob, bleb))
meta_workhorse <- function(mm, nn, oo, FUN_list, ARGS_list) {
meta_result <- sapply(1:length(FUN_list), function(x) {
workhorse(mm, nn, oo, get(FUN_list[x]), ARGS_list[x], ...)
}
}
The problem is that each of the sub-functions take different arguments, so I need to link the function name going into the get() function with the corresponding list of arguments in ARGS_list. I've tried everything I could think of, including do.calls, setting formals(workhorse), to no avail. How do I pass the arguments in this case?
EDIT: Nicola's answer solved most of the issue, but still have one subfuction (using a packaged function) keeps giving the error "unused argument ... = list(x, y)". Looking for some suggestions while I dig through previous ellipsis questions.