我有一个
shiny
应用程序,它分布在多个位置,用户需要选择数据集和列。在其中一个选项卡中,我有一个 pickerInput
和 actionButton
,应该更新 dataset
和 column
。它有一个副作用,当我运行它时,dataset
和column
会更新,但随后单个observeEvents
并运行,并且列的pickerInputs
变为空白。我怎样才能避免这种情况?
library(shiny)
ui <- fluidPage(
tags$h1("Dataset1"),
pickerInput("select_dataset1", "Dataset:", choices = c("iris", "mtcars", "ToothGrowth")),
pickerInput("select_columns1", "Columns:", c(), multiple = TRUE),
tags$h1("Dataset2"),
pickerInput("select_dataset2", "Dataset:", choices = c("iris", "mtcars", "ToothGrowth")),
pickerInput("select_columns2", "Columns:", c(), multiple = TRUE),
tags$br(),
tags$h1("Update All"),
pickerInput("select_dataset3", "Dataset:", choices = c("iris", "mtcars", "ToothGrowth")),
pickerInput("select_columns3", "Columns:", c(), multiple = TRUE),
actionButton("update_all", "Update All")
)
server <- function(input, output, session) {
observeEvent(input$select_dataset1, {
updatePickerInput(session, "select_columns1", choices = colnames(get(input$select_dataset1)))
})
observeEvent(input$select_dataset2, {
updatePickerInput(session, "select_columns2", choices = colnames(get(input$select_dataset2)))
})
observeEvent(input$select_dataset3, {
updatePickerInput(session, "select_columns3", choices = colnames(get(input$select_dataset3)))
})
observeEvent(input$update_all, {
dataset <- input$select_dataset3
columns <- input$select_columns3
updatePickerInput(session, "select_dataset1", selected = dataset)
updatePickerInput(session, "select_dataset2", selected = dataset)
updatePickerInput(session, "select_columns1", selected = columns)
updatePickerInput(session, "select_columns2", selected = columns)
})
}
shinyApp(ui, server)
我正在使用的一个解决方法是
reactiveVal
,但对其他解决方案感到好奇。
library(shiny)
ui <- fluidPage(
tags$h1("Dataset1"),
pickerInput("select_dataset1", "Dataset:", choices = c("iris", "mtcars", "ToothGrowth")),
pickerInput("select_columns1", "Columns:", c(), multiple = TRUE),
tags$h1("Dataset2"),
pickerInput("select_dataset2", "Dataset:", choices = c("iris", "mtcars", "ToothGrowth")),
pickerInput("select_columns2", "Columns:", c(), multiple = TRUE),
tags$br(),
tags$h1("Update All"),
pickerInput("select_dataset3", "Dataset:", choices = c("iris", "mtcars", "ToothGrowth")),
pickerInput("select_columns3", "Columns:", c(), multiple = TRUE),
actionButton("update_all", "Update All")
)
server <- function(input, output, session) {
picker1 <- reactiveVal(NULL)
picker2 <- reactiveVal(NULL)
observeEvent(input$select_dataset1, {
updatePickerInput(session, "select_columns1", choices = colnames(get(input$select_dataset1)), selected = picker1())
picker1(NULL)
})
observeEvent(input$select_dataset2, {
updatePickerInput(session, "select_columns2", choices = colnames(get(input$select_dataset2)), selected = picker2())
picker2(NULL)
})
observeEvent(input$select_dataset3, {
updatePickerInput(session, "select_columns3", choices = colnames(get(input$select_dataset3)))
})
observeEvent(input$update_all, {
dataset <- input$select_dataset3
columns <- input$select_columns3
updatePickerInput(session, "select_dataset1", selected = dataset)
updatePickerInput(session, "select_dataset2", selected = dataset)
updatePickerInput(session, "select_columns1", selected = columns)
updatePickerInput(session, "select_columns2", selected = columns)
picker1(columns)
picker2(columns)
})
}
shinyApp(ui, server)