【问题标题】:Make an eventReactive execute within a Shiny module在 Shiny 模块中执行 eventReactive
【发布时间】:2019-08-03 01:45:16
【问题描述】:

我有一个 selectInput UI 对象,一旦它用于从下拉选项中选择一个条目,我想读取一个 RDS 文件。 selectInput 的选择是指向不同RDS 文件的路径。 UI 模块工作正常,但服务器模块却不行。我得到input$studyinput$dataset1,然后一旦我从 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 }) )


}

【问题讨论】:

    标签: r shiny shiny-server shiny-reactivity shinyapps


    【解决方案1】:

    我会查看withProgressProgress 的文档。 withProgress 用于在循环内运行的任务。 https://shiny.rstudio.com/reference/shiny/1.2.0/Progress.html

    另外,请参阅此模块示例:https://shiny.rstudio.com/articles/modules.html。为了使数据帧作为模块外部的反应值返回,它应该在模块内部创建为反应对象,然后按原样返回。此外,因为input$dataset1sce1 所依赖的唯一反应值,所以可以使用reactive 代替eventReactiveeventReactive 更适合于输入,例如在反应式表达式中实际未使用的按钮,而只是作为表达式执行的触发器。

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

    【讨论】:

    • withProgress 在其他模块中工作正常,所以我认为这不是问题。另外,你在'return(df)'中返回什么数据框?我使用了您的解决方案,保留了进度并将 return(df) 更改为 return(dataset1),效果很好。谢谢!
    • 对不起,这是我的错误。在本地使用示例数据集运行它时,我已将名称从 dataset1 更改为 df,但在发布答案时未能将其改回。我在试用时无法让withProgress 以正确的时间工作,所以我使用了progress 以便我可以手动控制进度。如果我的回答对你有用,请接受 - stackoverflow.com/help/someone-answers.
    【解决方案2】:

    已解决

    我在模块函数中使用了如下:

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

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 2019-03-16
      相关资源
      最近更新 更多