r - 闪亮的未来():ctx$onInvalidate 中的错误:反应性上下文是在一个进程中创建并从另一个进程访问的
问题描述
我有下面的服务器代码,当使用 future() 而它在没有未来/承诺的情况下工作时会给出错误。这困扰了我很长时间,无法找出错误的根源。反应值 qt_files$file1
,qt_files$file2
和ile_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())))
})
解决方案
无法从具有期货的不同进程中读取或设置反应性值。查看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)
推荐阅读
- angular - 有角度的,可观察的管道限制?
- snowflake-cloud-data-platform - 雪花不支持的授权类型
- laravel - appleboy/ssh-action 没有这样的设备或地址错误
- c++ - 在 vkDeviceWait 空闲后使用 Vulkan CommandBuffer
- easy-digital-downloads - 绕过添加到购物车和结帐以轻松进行数字下载
- python - 织物获取-'ascii'编解码器无法解码位置 44 中的字节 0xbd:序数不在范围内
- sql - 是否可以在 Clickhouse 中动态选择表和列的内容?
- spring-boot - 如何在 Webflux 中正确获取 InputStreamResource 到 ResponseEntity?
- javascript - JavaScript:将对象数组转换为对象
- javascript - 从 HTML 输入中获取值并用于 javascript 函数