Shiny future() : Error in ctx$onInvalidate: Reactive context was created in one process and accessed from another

crossposting from https://stackoverflow.com/questions/53079904/shiny-future-error-in-ctxoninvalidate-reactive-context-was-created-in-one

I have the below server code which is giving the error when future() is used while it works without future/promises. The reactive values qt_files$file1 , qt_files$file2 and ile_ped$ped are called in both the future() functions. Could this be the source of error?

server <- function(input, output, session) {   
dom_content <- reactiveVal()

observeEvent(input$dom_run, { 
prog <- Progress$new(session) 
prog$set(message = "Analysis in progress", 
         detail = "This may take a while...", 
         value = NULL) 

qt_files <- gqt_list()  ###calling reactive values to be used in future
ile_ped <-   ed_file() 

future({
system("cat qt_files$file1 ile_ped$ped") 
system("cat qt_files$file2 ile_ped$ped")      
###the two system commands give the output "dom.gz" which is returned to R

dom_vcf <- vcfR::read.vcfR("dom.gz") 
dom_out <- cbind(vcfR::getFIX(dom_vcf,getINFO = TRUE), dom_vcf@gt) 
dom_out 
}) %...>% 
dom_content() %>%
finally(~prog$close()) 
return(NULL)
})

observeEvent(dom_content(), {
updateTabsetPanel(session, "dom_tab", "dom_results")
output$dom_table <-
  DT::renderDataTable(DT::datatable(
    req(dom_content())
  ))
output$dom_summary <- renderText(paste("Total filtered:",nrow(dom_content())))
})

rec_content <- reactiveVal()

observeEvent(input$rec_run, { 
prog <- Progress$new(session) 
prog$set(message = "Analysis in progress", 
         detail = "This may take a while...", 
         value = NULL) 

qt_files <- gqt_list()  ###calling reactive values to be used in future
ile_ped <-   ed_file() 

future({
system("cat qt_files$file1 ile_ped$ped") 
system("cat qt_files$file2 ile_ped$ped")      
###the two system commands give the output "rec.gz" which is returned to R

rec_vcf <- vcfR::read.vcfR("rec.gz") 
rec_out <- cbind(vcfR::getFIX(rec_vcf,getINFO = TRUE), rec_vcf@gt) 
rec_out 
}) %...>% 
rec_content() %>%
finally(~prog$close()) 
return(NULL)
})

observeEvent(rec_content(), {
updateTabsetPanel(session, "rec_tab", "rec_results")
output$rec_table <-
  DT::renderDataTable(DT::datatable(
    req(rec_content())
  ))
output$rec_summary <- renderText(paste("Total filtered:",nrow(rec_content())))
  })
}

Error in ctx$onInvalidate: Reactive context was created in one process and accessed from another

I found that using plan(multiprocess) gives this error. When the app is run without this line and restarting R it works. Im currently working on a Mac with 4 cores. Could someone throw some light on how to interpret this.

I figured out that the reactive values used in system() commands within future are the assigned to same variable name and it collapses when plan(multiprocess) is used.

qt_files <- gqt_list()  
ile_ped <-   ed_file() 

In my code gqt_list() and ed_file() are two reactive values defined inside the server function. And these two reactive values are called inside the two future() with the same names qt_files and ile_ped

First future()

future({
system("cat qt_files$file1 ile_ped$ped") 
system("cat qt_files$file2 ile_ped$ped") 
})  

Second future()

future({
system("cat qt_files$file1 ile_ped$ped") 
system("cat qt_files$file2 ile_ped$ped") 
})

When the reactive values are assigned to a different variable then it worked:

fut1_qt_files <- gqt_list()  
 fut1_ile_ped <-   ed_file()

future({
system("cat fut1_qt_files$file1 ile_ped$ped") 
system("cat fut1_qt_files$file2 ile_ped$ped") 
}) 

 fut2_qt_files <- gqt_list()  
 fut2_ile_ped <-   ed_file()

future({
system("cat fut2_qt_files$file1 ile_ped$ped") 
system("cat fut2_qt_files$file2 ile_ped$ped") 
})  

This is what i figured out and the issue seems to be small but it took a week to rectify this. I hope it would save some time for others. Would be good if someone could comment more technically if this sounds like a correct traceback.

1 Like

I figured out that the reactive values used in system() commands are called within future and reactive values and reactive expressions cannot be read from within a future.

Here is the shiny-specific Caveats and Limitations explantion under "Shiny-specific Caveats and Limitations". I hope this helps someone.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.