代码之家  ›  专栏  ›  技术社区  ›  www

如果选择依赖于另一个输入且服务器=TRUE,则shinyStore无法还原selectizeInput的选定值

  •  0
  • www  · 技术社区  · 4 年前

    这是这个问题的后续问题( shinyStore cannot restore the selected values of the selectizeInput if the choices are depends on another input )我以前问过。我已经找到了答案( https://stackoverflow.com/a/68290227/7669809 server = TRUE 第一次 updateSelectizeInput ,这使本地存储无法工作。如果我能用 服务器=真 因为在我的现实世界的例子中 selectizeInput 很多。

    ### This script creates an example of the shinystore package
    
    # Load packages
    library(shiny)
    library(shinyStore)
    
    ui <- fluidPage(
      headerPanel("shinyStore Example"),
      sidebarLayout(
        sidebarPanel = sidebarPanel(
          initStore("store", "shinyStore-ex1"),
          selectizeInput(inputId = "Select1", label = "Select A Number",
                         choices = as.character(1:3),
                         options = list(
                           placeholder = 'Please select a number',
                           onInitialize = I('function() { this.setValue(""); }'),
                           create = TRUE
                         ))
        ),
        mainPanel = mainPanel(
          fluidRow(
            selectizeInput(inputId = "Select2", 
                           label = "Select A Letter",
                           choices = character(0),
                           options = list(
                             placeholder = 'Please select a number in the sidebar first',
                             onInitialize = I('function() { this.setValue(""); }'),
                             create = TRUE
                           )),
            actionButton("save", "Save", icon("save")),
            actionButton("clear", "Clear", icon("stop"))
          )
        )
      )
    )
    
    server <- function(input, output, session) {
      
      dat <- data.frame(
        Number = as.character(rep(1:3, each = 3)),
        Letter = letters[1:9]
      )
      
      observeEvent(input$Select1, {
        updateSelectizeInput(session, inputId = "Select2", 
                             choices = dat$Letter[dat$Number %in% input$Select1],
                             # Add server = TRUE make the local storage not working
                             server = TRUE)
      }, ignoreInit = TRUE)
      
      observe({
        if (input$save <= 0){
          updateSelectizeInput(session, inputId = "Select1", selected = isolate(input$store)$Select1)
        }
      })
      
      observe({
        if (input$save <= 0){
          req(input$Select1)
          updateSelectizeInput(session, inputId = "Select2", selected = isolate(input$store)$Select2)
        }
      })
      
      observe({
        if (input$save > 0){
          updateStore(session, name = "Select1", isolate(input$Select1))
          updateStore(session, name = "Select2", isolate(input$Select2))
        }
      })
    
      observe({
        if (input$clear > 0){
          updateSelectizeInput(session, inputId = "Select1",
                               options = list(
                                 placeholder = 'Please select a number',
                                 onInitialize = I('function() { this.setValue(""); }'),
                                 create = TRUE
                               ))
          updateSelectizeInput(session, inputId = "Select2",
                               choices = character(0),
                               options = list(
                                 placeholder = 'Please select a number in the sidebar first',
                                 onInitialize = I('function() { this.setValue(""); }'),
                                 create = TRUE
                               ))
    
          updateStore(session, name = "Select1", NULL)
          updateStore(session, name = "Select2", NULL)
        }
      })
    }
    
    shinyApp(ui, server)
    
    0 回复  |  直到 4 年前