根据另一个选择器更新所有选择器

问题描述 投票:0回答:1

我有一个

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)
r shiny
1个回答
0
投票

我正在使用的一个解决方法是

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)
© www.soinside.com 2019 - 2024. All rights reserved.