0

I've read similar questions on Stack Overflow but do not seem to be able to get my code to work correctly. I've reduced this to a reprex.

I'm trying to allow the user to select a table, and based on that table, select one of the columns associated with the selected table.

One problem I see is that there's no way to immediately populate the column selector (before choosing a table from the table selector) when the app launches. Also, the column selector is simply not populating with columns no matter what I do. Thanks for any help you can give me.

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)
wdchild
  • 51
  • 7

1 Answers1

0

You have many syntax issues. Try this

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)
YBS
  • 19,324
  • 2
  • 9
  • 27
  • Your code works perfectly. I had forgotten you can use [[ ]] to unlist the list's element. The solution is simpler than I expected. Thanks! – wdchild Jan 24 '22 at 23:05