首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于另一个selectInput填充selectInput

基于另一个selectInput填充selectInput
EN

Stack Overflow用户
提问于 2022-01-21 22:27:01
回答 1查看 190关注 0票数 0

我读过类似的关于堆栈溢出的问题,但似乎无法让我的代码正确工作。我已经把这个简化成了重装。

我试图允许用户选择一个表,并在该表的基础上选择一个与所选表相关联的列。

我看到的一个问题是,当应用程序启动时,无法立即填充列选择器(在从表选择器中选择表之前)。而且,不管我做什么,列选择器都不会填充列。谢谢你能帮我的忙。

代码语言:javascript
复制
library(shiny)

tables <- c("A", "B", "C")
columns <- list(A = c("a1", "a2", "a3"), B = c("b1", "b2"), C = c("c1", "c2", "c3", "c4"))

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "table_selector", label = "Tables", choices = tables, selected = "A"),
      selectInput(inputId = "column_selector", label = "Columns", choices = "")),
    
    # Show a plot of the generated distribution
    mainPanel(
      textOutput("selected_table"),
      textOutput("selected_columns")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  rv <- reactiveValues("selected_cols" = unlist(columns$A))

  observeEvent(input$selected_table_selector, {
    message("Table event observed")
    rv$selected_cols <- selected_table[input$table_selector()]
    updateSelectInput("column_selector", choices = rv$selected_cols())
  })
  
  # Will use the following two query a database
  output$selected_table <- renderText({paste(input$table_selector)})
  output$selected_columns <- renderText({rv$selected_cols})
}

# Run the application 
shinyApp(ui = ui, server = server)
EN

回答 1

Stack Overflow用户

发布于 2022-01-21 23:03:14

您有许多语法问题。尝尝这个

代码语言:javascript
复制
library(shiny)

tables <- c("A", "B", "C")
columns <- list(A = c("a1", "a2", "a3"), B = c("b1", "b2"), C = c("c1", "c2", "c3", "c4"))

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "table_selector", label = "Tables", choices = tables, selected = "A"),
      selectInput(inputId = "column_selector", label = "Columns", choices = "")),
    
    # Show a plot of the generated distribution
    mainPanel(
      textOutput("selected_table"),
      verbatimTextOutput("selected_columns")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output,session) {
  #rv <- reactiveValues(selected_cols = NULL)
  
  observeEvent(input$table_selector, {
    message("Table event observed")
    choices <- columns[[input$table_selector]]
    updateSelectInput(session, "column_selector", choices = choices )
  })
  
  # Will use the following two query a database
  output$selected_table <- renderText({paste(input$table_selector)})
  output$selected_columns <- renderPrint({input$column_selector})
}

# Run the application 
shinyApp(ui = ui, server = server)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70808281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档