【发布时间】:2021-09-16 17:56:15
【问题描述】:
在这个例子 (https://yuchenw.shinyapps.io/shinystore_de/) 中,我设计了一个简单的应用程序,其中包含两个 selectizeInput。第二个selectizeInput 的选择取决于第一个selectizeInput 的选择结果。我的目标是设计一个使用 shinyStore 包 (https://github.com/trestletech/shinyStore) 的应用程序,以使用本地存储保存选定的值。下次当用户使用相同的网络浏览器打开应用程序时,我希望这些值保持不变。
但是,电流不工作。在下面的示例中,我首先选择2 作为第一个selectizeInput,然后选择f 作为第二个selectizeInput。
然后,我单击了Save 按钮,复制了 URL,并将其粘贴到新的 Web 浏览器中。第一个selectizeInput 的选定值如预期的那样是2,但selectizeInput 没有显示f。
我的猜测是我使用了两次updateSelectizeInput,第二次使用了selectizeInput,这会导致混淆。请查看我的代码如下,如果您有任何见解,请告诉我。
更新
这是我问的一个后续问题 (shinyStore cannot restore the selected values of the selectizeInput if the choices are depends on another input and server = TRUE),它使用 server = TRUE 表示 updateSelectizeInput,但同样的策略不起作用。
# 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),
selected = "1",
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])
})
observe({
if (input$save <= 0){
updateSelectizeInput(session, inputId = "Select1", selected = isolate(input$store)$Select1)
updateSelectizeInput(session, inputId = "Select2", selected = isolate(input$store)$Select2)
}
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 in the sidebar first',
onInitialize = I('function() { this.setValue(""); }'),
create = TRUE
))
updateSelectizeInput(session, inputId = "Select2",
choices = character(0),
options = list(
placeholder = 'Please select a number',
onInitialize = I('function() { this.setValue(""); }'),
create = TRUE
))
updateStore(session, name = "Select1", NULL)
updateStore(session, name = "Select2", NULL)
}
})
}
shinyApp(ui, server)
【问题讨论】:
标签: r shiny shinystore