I have the following function which is a much simpler version of the functions that I have but it pictures what I'm struggling to do.
I follow the discussion here Parallelize a function in R (not a loop!) which is a solution for a single argument for each function.
In my case I have the following function unparal which has multiple arguments that can be shared across the functions f_1,f_2,f_3,f_4
f_1 = function(x,y,z){
return(rnorm(z,x,y))
}
f_2 = function(x,y,z){
return(rgamma(z,x,y))
}
f_3 = function(x,y,z){
return(rbeta(z,x,y))
}
f_4 = function(x,y,z){
return(runif(z,x,y))
}
unparal = function(x,g,t,y,z){
res1 = f_1(x,g,z)
res2 = f_2(g,t,z)
res3 = f_3(t,y,z)
res4 = f_4(x,y,z)
res = c(res1,res2,res3,res4)
return(res)
}
What I would like to do is to parallelize the function unparal, i.e. to run all the functions f_1,f_2,f_3,f_4 in parallel. Is there a convenient way to do that?