首页 > 解决方案 > 闪亮的未来():ctx$onInvalidate 中的错误:反应性上下文是在一个进程中创建并从另一个进程访问的

问题描述

我有下面的服务器代码,当使用 future() 而它在没有未来/承诺的情况下工作时会给出错误。这困扰了我很长时间,无法找出错误的根源。反应值 qt_files$file1,qt_files$file2ile_ped$ped在 future() 函数中都被调用。这可能是错误的根源吗?

ctx$onInvalidate 中的错误:反应性上下文是在一个进程中创建并从另一个进程访问的

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())))
  })

标签: rasynchronousshinyshinydashboard

解决方案


无法从具有期货的不同进程中读取或设置反应性值。查看promises文档中的这些部分:

这是问题的一个例子:

library(shiny)
library(promises)
library(future)
plan(multiprocess)

server <- function(input, output) {
  counter <- reactiveVal(0)

  observeEvent(input$incrementBtn, {
    currentCount <- counter()
    future({
      counter(currentCount + 1)  # errors
    })
  })

  output$result <- renderText({
    counter()
  })
}

ui <- fluidPage(
  actionButton("incrementBtn", "Increment"),
  verbatimTextOutput("result")
)

shinyApp(ui, server)

要解决这个问题,您可以从未来返回结果,并在 Promise 处理程序中设置响应值(在主进程中运行)。就像是:

library(shiny)
library(promises)
library(future)
plan(multiprocess)

server <- function(input, output) {
  counter <- reactiveVal(0)

  observeEvent(input$incrementBtn, {
    currentCount <- counter()
    f <- future({
      currentCount + 1
    })

    f %...>% counter()
  })

  output$result <- renderText({
    counter()
  })
}

ui <- fluidPage(
  actionButton("incrementBtn", "Increment"),
  verbatimTextOutput("result")
)

shinyApp(ui, server) 

推荐阅读