0

I am trying to run an R loop in parallel with foreach. The idea is that the function called with foreach will take a certain function from a list and use that as a parameter for yet another function.

func_list <- c(a(), b(), c())
loop_func <- function(letter){
    obj <- f(parameter = letter)
    return(c(letter, obj)

foreach(val=func_list, .combine='rbind') %dopar% {
    loop_func(val)
} -> result

So I am trying to make the loop_func return an output (obj in this case), as well as what function it used as a parameter for the f() function. However, I can not seem to get that function as a string. I tried as.character(substitute(letter)), but that gave me "val".

Is there an intuitive way to get:

"a"    obj

returned from the function?

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • 1
    Please provide a minimal reproducible example, i.e., code that if copied into R can be executed without errors. 1. Your `func_list` doesn't appear to be a list of functions. In fact, it is most likely a vector with the return values of these functions. If `a()` doesn't return `"a"`, it's not possible for `loop_func` to get an `"a"`. 2. `loop_func` needs a closing `}` and should probably return a list if you intend to use `obj` later on. 3. How is `f` defined and what does it return? – Roland Apr 30 '21 at 15:42

1 Answers1

2

You could use foreach on a list of function names, and get the associated functions:

library(foreach)

loop_func <- function(funcname){
  obj <- f(get(funcname))
  return(list(funcname,obj))}

a <- function(x) x
b <- function(x) 2*x
d <- function(x) 3*x

f <- function(func) {func(1)}

func_list <- c("a", "b", "d")

result <- foreach(f = func_list, .combine='rbind') %dopar% {
  loop_func(f)
}

result

         [,1] [,2]
result.1 "a"  1   
result.2 "b"  2   
result.3 "d"  3 
Waldi
  • 39,242
  • 6
  • 30
  • 78
  • @Charles Meijer, did it answer your question? – Waldi May 03 '21 at 21:19
  • It did answer my question, but it was not applicable in my situation. I believe the problem was that my function did not return a certain value, like a() in the example. You could not have known this as I failed to mention that in my question :S. I ended up changing my list of functions to a range of numbers, created a list of functions as well as a list of function name strings within the loop and then just iterated using my list of numbers used in the foreach loop. So you did provide a great answer, but my question was not specific enough. Thank you for your help :D – Charles Meijer May 04 '21 at 10:53
  • Thanks for your feedback and glad you found a solution – Waldi May 04 '21 at 11:26
  • I get this error message: Error in { : task 1 failed - "could not find function "f"" – stats_noob Dec 11 '22 at 09:05
  • @stats_noob, I can't reproduce the error you mention – Waldi Dec 11 '22 at 19:33
  • @ Waldi: In general, I attempting to replicate your approach in this question: https://stackoverflow.com/questions/74759716/r-printing-individual-iterations-from-loops – stats_noob Dec 11 '22 at 19:38