4

我设计了一个带有两个 selectizeInputs的简单闪亮应用程序( https://yuchenw.shinyapps.io/selectizeinput_bookmark/ )。第二个 selectizeInput 的选择基于第一个的选择。例如,如果我在“选择字母”中选择“B”,则“选择数字”的可用选项变为“B-1”、“B-2”和“B-3”。

这很好用。但是当我添加书签按钮时,我无法恢复第二个 selectizeInput,只能恢复第一个。例如,如果我如下选择“B”和“B-3”,然后单击“书签”按钮,我得到一个 URL ( https://yuchenw.shinyapps.io/selectizeinput_bookmark/?_inputs_&sidebarCollapsed=false&Example=% 22Page%22&sidebarItemExpanded=null&_values_&Number_Select=%22B-3%22&Letter_Select=%22B%22 )。

在此处输入图像描述

当我将 URL 复制并粘贴到 Web 浏览器时,第一个是预期的“B”,但第二个是“B-1”。

在此处输入图像描述

我认为这与我的设置有关updateSelectizeInput,但我不知道如何修复它。下面是完整的代码。

library(shiny)
library(shinydashboard)

dat <- data.frame(
  Letter = rep(c("A", "B", "C"), each = 3),
  Number = paste(rep(c("A", "B", "C"), each = 3),
                 rep(1:3, times = 3),
                 sep = "-")
)

ui <- function(request){
  dashboardPage(
    header = dashboardHeader(title = ""),
    sidebar = dashboardSidebar(
      sidebarMenu(
        id = "Example",
        menuItem(
          text = "Page",
          tabName = "Page"
          )),
      bookmarkButton()),
    body = dashboardBody(
      tabItems(
        tabItem(
        tabName = "Page",
        h2("SelectizeInput Bookmark Example"),
        fluidRow(
          box(
            title = "", status = "primary", solidHeader = TRUE, width = 6, collapsible = TRUE,
            selectizeInput(inputId = "Letter_Select", 
                           label = "Select a letter",
                           choices = c("A", "B", "C"),
                           options = list(
                             placeholder = 'Please select a letter below',
                             onInitialize = I('function() { this.setValue(""); }'),
                             create = TRUE)
            ),
            selectizeInput(inputId = "Number_Select", 
                           label = "Select a number",
                           choices = paste(rep(c("A", "B", "C"), each = 3),
                                           rep(1:3, times = 3),
                                           sep = "-"),
                           options = list(
                             placeholder = 'Please select a number below',
                             onInitialize = I('function() { this.setValue(""); }'),
                             create = TRUE)
            )
          )
        )
        )
      )
    )
  )
}

server <- function(input, output, session){
  
  observeEvent(c(input$Letter_Select), {
    updateSelectizeInput(session, inputId = "Number_Select", 
                         label = "Select a number",
                         choices = dat$Number[dat$Letter %in% input$Letter_Select],
                         server = TRUE)
  },
  ignoreInit = TRUE)
  
  # List of selectizeinput
  selectize_inputs <- c("Letter_Select", "Number_Select")
  
  setBookmarkExclude(names = selectize_inputs)
  
  onBookmark(function(state){
    for (selectize_input in selectize_inputs){
      state$values[[selectize_input]] <- isolate({input[[selectize_input]]})
    }
  })
  
  onRestore(function(state){
    updateSelectizeInput(session, inputId = "Letter_Select", 
                         choices = c("A", "B", "C"),
                         selected = state$values[["Letter_Select"]],
                         server = TRUE)
    updateSelectizeInput(session, inputId = "Number_Select", 
                         choices = dat$Number[dat$Letter %in% state$values[["Letter_Select"]]],
                         selected = state$values[["Number_Select"]],
                         server = TRUE)
    
  })
}

shinyApp(ui, server, enableBookmarking = "url")

请注意,我必须使用server = TRUEselectizeInput,因为我的实际应用程序有许多需要显示的选择。我还需要使用shinydashboardand enableBookmarking = "url。请让我知道你的想法。

4

1 回答 1

2

这是由于observeEvent(c(input$Letter_Select),...)完成后触发的,onRestore以及重置对 所做的更改Number_select

reactiveVal恢复状态后,您可以使用 a来避免此重置:

server <- function(input, output, session){
  
  restoreState <- reactiveVal(F)
  
  observeEvent(c(input$Letter_Select), {
    if (!restoreState()){    
      updateSelectizeInput(session, inputId = "Number_Select", 
                         label = "Select a number",
                         choices = dat$Number[dat$Letter %in% input$Letter_Select],
                         server = TRUE)
    }
    restoreState(F)
  },
  ignoreInit = TRUE)
  
  # List of selectizeinput
  selectize_inputs <- c("Letter_Select", "Number_Select")
  
  setBookmarkExclude(names = selectize_inputs)
  
  onBookmark(function(state){
    for (selectize_input in selectize_inputs){
      state$values[[selectize_input]] <- isolate({input[[selectize_input]]})
    }
  })
  
  onRestore(function(state){
    updateSelectizeInput(session, inputId = "Letter_Select", 
                         choices = c("A", "B", "C"),
                         selected = state$values[["Letter_Select"]],
                         server = TRUE)

    updateSelectizeInput(session, inputId = "Number_Select", 
                         choices = dat$Number[dat$Letter %in% state$values[["Letter_Select"]]],
                         selected = state$values[["Number_Select"]],
                         server = TRUE)
    restoreState(T)
  })
}

shinyApp(ui, server, enableBookmarking = "url")
于 2021-05-28T20:10:28.353 回答