如何在调用onRestored()时顺序执行多个从属updateSelectInput()

时间:2018-07-16 23:58:22

标签: r shiny

我正在尝试在闪亮的应用程序中使用书签,但是在正确恢复selectInput()值时遇到了麻烦。

library(shiny)

shinyApp(
  function(req) {
    fluidPage(
      fileInput("file",
                "Upload",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv"
                )
      ),
      selectInput("select", "Select", choices = NULL),
      selectInput("select2", "Select2", choices = NULL),
      bookmarkButton()
    )
  },

  function(input, output, session) {
    observeEvent(input$file, {
      updateSelectInput(session, "select", 
                        choices = c("a", "b", "c"),
                        selected = "a"
      )
    })

    observeEvent(input$select, {
      vars <- c("a", "b", "c")
      vars <- vars[which(!vars %in% c(input$select))]
      updateSelectInput(session, "select2",
                        choices = vars,
                        selected = vars[1])
    })

    onRestored(function(state) {
      updateSelectInput(session, "select", selected = state$input$select)
      updateSelectInput(session, "select2", selected = state$input$select2)
    })
  },
  enableBookmarking = "server"
)

恢复应用状态可正确更新input$select,但是即使在书签添加input$select2时将其恢复为“ b”,也可以恢复为{b“。

0 个答案:

没有答案