在 Shiny 模块中执行 eventReactive
Make an eventReactive execute within a Shiny module
我有一个 selectInput UI 对象,我希望一旦它被用于 select 下拉选项中的一个条目,就可以读取一个 RDS 文件。 selectInput
的选择是指向不同 RDS
文件的路径。 UI 模块工作正常但服务器模块没有。我得到 input$study
,因此 input$dataset1
,然后一旦我 select 来自 input$datasets1 的条目,应用程序应该开始读取 RDS 文件,但它没有。
如何将模块内的 eventReactive 表达式触发到 运行,然后使该 RDS
文件可用于整个应用程序以供其他模块使用?
load_sce <- function(input, output, session) {
output$sce_objects <- renderUI({
validate(need(input$study, message = FALSE))
withProgress(message = "Getting SCE objects...", {
objects <- FIND SOME FILES
ns <- session$ns
selectInput(inputId = ns("dataset1"),
label = "Select a specifc analysis",
width = "100%",
choices = c("", objects),
selected = "")
})
})
sce1 <- eventReactive(input$dataset1, {
validate(need(input$dataset1, message = FALSE))
withProgress(message = "Reading data...", { readRDS(input$dataset1) })
})
return( reactive({ sce1 }) )
}
我会查看 withProgress
和 Progress
的文档。 withProgress
适用于在循环内运行的任务。 https://shiny.rstudio.com/reference/shiny/1.2.0/Progress.html
另外,请参阅此模块示例:https://shiny.rstudio.com/articles/modules.html。为了使数据帧作为模块外部的反应值返回,它应该在模块内部创建为反应对象,然后按原样返回。此外,由于 input$dataset1
是 sce1
所依赖的唯一反应值,因此可以使用 reactive
代替 eventReactive
。 eventReactive
更适合输入,例如在反应式表达式中实际未使用的按钮,而只是作为表达式执行的触发器。
load_sce <- function(input, output, session) {
output$sce_objects <- renderUI({
validate(need(input$study, message = FALSE))
objects <- FIND SOME FILES
ns <- session$ns
selectInput(inputId = ns("dataset1"),
label = "Select a specifc analysis",
width = "100%",
choices = c("", objects),
selected = "")
})
sce1 <- reactive({
validate(need(input$dataset1, message = FALSE))
progress <- Progress$new(session, min=0, max=1)
on.exit(progress$close())
progress$set(message = 'Reading data...')
dataset1 <- readRDS(input$dataset1)
progress$set(value = 1)
return(df)
})
return(sce1)
}
已解决
我在模块函数中使用了以下内容:
sce1 <- reactive({
validate(need(input$dataset1, message = FALSE))
withProgress(message = "Reading data...", {
dataset1 <- readRDS(input$dataset1)
}) # withProgress
return(dataset1)
}) # reactive
return(sce1)
并使用以下方法在主应用程序中调用模块:
sce1 <- callModule(load_sce, "load_sce_explore")
现在我可以将 sce1
作为函数参数传递给其他模块(使用 sce1
而不是 sce1()
)或在主应用程序的其他代码段中使用它(但在本例使用 sce1()
).
谢谢
我有一个 selectInput UI 对象,我希望一旦它被用于 select 下拉选项中的一个条目,就可以读取一个 RDS 文件。 selectInput
的选择是指向不同 RDS
文件的路径。 UI 模块工作正常但服务器模块没有。我得到 input$study
,因此 input$dataset1
,然后一旦我 select 来自 input$datasets1 的条目,应用程序应该开始读取 RDS 文件,但它没有。
如何将模块内的 eventReactive 表达式触发到 运行,然后使该 RDS
文件可用于整个应用程序以供其他模块使用?
load_sce <- function(input, output, session) {
output$sce_objects <- renderUI({
validate(need(input$study, message = FALSE))
withProgress(message = "Getting SCE objects...", {
objects <- FIND SOME FILES
ns <- session$ns
selectInput(inputId = ns("dataset1"),
label = "Select a specifc analysis",
width = "100%",
choices = c("", objects),
selected = "")
})
})
sce1 <- eventReactive(input$dataset1, {
validate(need(input$dataset1, message = FALSE))
withProgress(message = "Reading data...", { readRDS(input$dataset1) })
})
return( reactive({ sce1 }) )
}
我会查看 withProgress
和 Progress
的文档。 withProgress
适用于在循环内运行的任务。 https://shiny.rstudio.com/reference/shiny/1.2.0/Progress.html
另外,请参阅此模块示例:https://shiny.rstudio.com/articles/modules.html。为了使数据帧作为模块外部的反应值返回,它应该在模块内部创建为反应对象,然后按原样返回。此外,由于 input$dataset1
是 sce1
所依赖的唯一反应值,因此可以使用 reactive
代替 eventReactive
。 eventReactive
更适合输入,例如在反应式表达式中实际未使用的按钮,而只是作为表达式执行的触发器。
load_sce <- function(input, output, session) {
output$sce_objects <- renderUI({
validate(need(input$study, message = FALSE))
objects <- FIND SOME FILES
ns <- session$ns
selectInput(inputId = ns("dataset1"),
label = "Select a specifc analysis",
width = "100%",
choices = c("", objects),
selected = "")
})
sce1 <- reactive({
validate(need(input$dataset1, message = FALSE))
progress <- Progress$new(session, min=0, max=1)
on.exit(progress$close())
progress$set(message = 'Reading data...')
dataset1 <- readRDS(input$dataset1)
progress$set(value = 1)
return(df)
})
return(sce1)
}
已解决
我在模块函数中使用了以下内容:
sce1 <- reactive({
validate(need(input$dataset1, message = FALSE))
withProgress(message = "Reading data...", {
dataset1 <- readRDS(input$dataset1)
}) # withProgress
return(dataset1)
}) # reactive
return(sce1)
并使用以下方法在主应用程序中调用模块:
sce1 <- callModule(load_sce, "load_sce_explore")
现在我可以将 sce1
作为函数参数传递给其他模块(使用 sce1
而不是 sce1()
)或在主应用程序的其他代码段中使用它(但在本例使用 sce1()
).
谢谢