闪亮的书签无法恢复 selectizeinput
Shiny bookmark cannot restore the selectizeinput
我设计了一个带有两个 selectizeInputs 的简单闪亮应用程序 (https://yuchenw.shinyapps.io/selectizeinput_bookmark/)。第二个 selectizeInput 的选择基于第一个的 selection。例如,如果我在“Select一个字母”中select“B”,“Select一个数字”的可用选项变为“B-1”,“B-2”,和“B-3”。
这很好用。但是当我添加书签按钮时,我无法恢复第二个 selectizeInput,只能恢复第一个。例如,如果我select“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 复制并粘贴到网络浏览器时,第一个是预期的“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")
请注意,我必须对 selectizeInput 使用 server = TRUE
,因为我的实际应用程序有许多需要显示的选项。我还需要使用 shinydashboard
和 enableBookmarking = "url
。请告诉我您的想法。
这是由于 observeEvent(c(input$Letter_Select),...)
在 onRestore
完成后触发并重置对 Number_select
所做的更改。
您可以使用 reactiveVal
来避免在恢复状态后重新设置:
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")
我设计了一个带有两个 selectizeInputs 的简单闪亮应用程序 (https://yuchenw.shinyapps.io/selectizeinput_bookmark/)。第二个 selectizeInput 的选择基于第一个的 selection。例如,如果我在“Select一个字母”中select“B”,“Select一个数字”的可用选项变为“B-1”,“B-2”,和“B-3”。
这很好用。但是当我添加书签按钮时,我无法恢复第二个 selectizeInput,只能恢复第一个。例如,如果我select“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 复制并粘贴到网络浏览器时,第一个是预期的“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")
请注意,我必须对 selectizeInput 使用 server = TRUE
,因为我的实际应用程序有许多需要显示的选项。我还需要使用 shinydashboard
和 enableBookmarking = "url
。请告诉我您的想法。
这是由于 observeEvent(c(input$Letter_Select),...)
在 onRestore
完成后触发并重置对 Number_select
所做的更改。
您可以使用 reactiveVal
来避免在恢复状态后重新设置:
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")