[英]shinyStore cannot restore the selected values of the selectizeInput if the choices depend on another input and server = TRUE
[英]shinyStore cannot restore the selected values of the selectizeInput if the choices are depends on another input
在這個例子中( https://yuchenw.shinyapps.io/shinystore_de/ ),我設計了一個帶有兩個selectizeInput
的簡單應用程序。 第二個selectizeInput
的選擇取決於第一個selectizeInput
的選擇結果。 我的目標是設計一個使用shinyStore
包 ( https://github.com/trestletech/shinyStore ) 使用本地存儲保存所選值的應用程序。 下次當用戶使用相同的網絡瀏覽器打開應用程序時,我希望這些值保持不變。
但是,電流不起作用。 在以下示例中,我首先為第一個selectizeInput
選擇了2
,然后為第二個selectizeInput
選擇了f
。
然后我單擊“ Save
按鈕,復制 URL,並將其粘貼到新的 Web 瀏覽器中。 第一個selectizeInput
的選定值按預期為2
,但selectizeInput
未顯示f
。
我的猜測是我對第二個selectizeInput
使用了兩次updateSelectizeInput
,這會導致混淆。 請查看我的代碼如下,如果您有任何見解,請告訴我。
更新
這是一個后續問題(如果選擇取決於另一個輸入並且 server = TRUE ,那么 ShinyStore 無法恢復 selectizeInput 的選定值)我問,它使用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)
server
腳本的以下修改似乎有效。 我對兩個selectizeInput
使用了單獨的observe
調用。
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])
}, 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)
}
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.