Appending a global table using asynchronous futures in R

吃可爱长大的小学妹 提交于 2019-12-24 10:47:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!