问题
I am trying to create a global table that is produced by asynchronously running parallel processes. They are completely independent, however they should append to the same global variable (this is reactive in R shiny so I either need to have a call back function once all futures are done with their task -which would be very nice but I dont know how-, or I need to constantly update the table as new results come in).
I tried the following approach which just locks (probably because all processes are assigning to the same variable, when I change 'a' to 'b' it works but then result is useless)
library("listenv")
library("future")
plan(multiprocess)
futureVals <- listenv()
options(future.globals.onMissing = "ignore")
a<-0
b<-0
for(i in 1:5){
futureVals[[i]] <- futureAssign(x='a', value={
a <- a+1
print(a)
})
}
futureVals2 <- as.list(futureVals)
print(a)
how can I achieve this goal?
回答1:
It is not possible for future (or other parallel, background R workers) to assign values to variables in the master R process. Any results need to be returned as values. This is a fundamental property of all parallel/asynchronous processing in R.(*)
Having said this, you might be interested in https://rstudio.github.io/promises/articles/shiny.html.
PS. (*) Your expectations of futureAssign()
seems to be incorrect.
来源:https://stackoverflow.com/questions/49712010/appending-a-global-table-using-asynchronous-futures-in-r