gpt4 book ai didi

r - Shiny 书签无法恢复selectizeinput

转载 作者:行者123 更新时间:2023-12-04 12:55:57 29 4
gpt4 key购买 nike

我设计了一个带有两个 selectizeInputs 的简单 Shiny 应用程序 (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 )。
enter image description here
当我将 URL 复制并粘贴到 Web 浏览器时,第一个是“B”,但第二个是“B-1”。
enter image description here
我觉得和我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 = TRUE对于 selectizeInput,因为我的实际应用程序有很多我需要展示的选择。我还需要使用 shinydashboardenableBookmarking = "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")

关于r - Shiny 书签无法恢复selectizeinput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67708041/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com