我有下面的服务器代码,当在没有Future / Promise的情况下使用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())))
})
答案 0 :(得分:1)
无法通过期货的其他过程读取或设置反应性值。在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)
答案 1 :(得分:0)
我发现system()命令中使用的反应性值在将来被调用,而反应性值和反应性表达式不能在将来被读取。
这是“ {Shiny-specific警告和限制”下的shiny-specific Caveats and Limitations移出。我希望这对某人有帮助。