1

How do I retrieve outputs from objects in an array as described in the background?

I have a function in R that returns multiple variables. For eg. if my function is called function_ABC,then:

 a<-function_ABC (input_var)

gives a such that a$var1, a$var2, and a$var3 exist.

I have multiple cases to run such that I have put then in an array:

input_var <- c(1, 2, ...15)

for storing the outputs, I declared var such that:

var <- c(v1, v2, v3, .... v15)

Then I run:

assign(v1[i],function(input_var(i)))

However, after that I am unable to access these variables as v1[1]$var1. I can access them as: v1$var1, or v3$var1, etc. But this means I need to write 15*3 commands to retrieve my output.

Is there an easier way to do this?

bbiasi
  • 1,549
  • 2
  • 15
  • 31
  • 3
    Please provide a minimally reproducible example. – akash87 May 17 '19 at 14:46
  • It is difficult to discern where exactly what your issue is. You don't give an example of your expected output, or any sample data you're coding with. I think both of these would be helpful when soliciting answers. I suspect you should be thoughtfully applying `lapply` here, but that's just a hunch at this point. – zack May 17 '19 at 14:48
  • You can't store complex objects in a vector (using `c()`), you need a proper R `list()`. You should not really be using `assign()` with most normal R code. Again, you can store things in a named list to make it much easier to organize data. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 17 '19 at 14:49

1 Answers1

0
  1. Push your whole input set into an array Arr[ ].
  2. Open a multi threaded executor E of certain size N.
  3. Using a for loop on the input array Arr[], submit your function calls as a Callable job to the executor E. While submitting each job, hold the reference to the FutureTask in another Array FTArr[ ].
  4. When all the FutureTask jobs are executed, you may retrieve the output for each of them by running another for loop on FTArr[ ].

Note : • make sure to add synchronized block in your func_ABC, where you are accessing shared resources to avoid deadlocks. • Please refer to the below link, if you want to know more about the usage of a count-down-latch. A count-down-latch helps you to find out, when exactly, all the child threads have finished execution. https://www.geeksforgeeks.org/countdownlatch-in-java/

Subhadeep Ray
  • 909
  • 6
  • 6