Quantcast
Viewing latest article 2
Browse Latest Browse All 3

Answer by Victorp for How to dynamically populate dropdown box choices in shiny dashboard

Use updateSelectInput in your server like below and set choices = NULL in your ui :

function(input, output, session) {  # If this isn't reactive you can put it in your global  choices_cities <- final_data %>%    group_by(registrant_city) %>%    summarise(Total = n())    %>%    arrange(desc(Total))      %>%    top_n(n = 10)    updateSelectInput(session = session, inputId = "cities", choices = choices_cities$registrant_city)}

Or if final_data is reactive something like this :

function(input, output, session) {  choices_cities <- reactive({    final_data %>%      group_by(registrant_city) %>%      summarise(Total = n())    %>%      arrange(desc(Total))      %>%      top_n(n = 10)    })  observeEvent(choices_cities(), {    updateSelectInput(session = session, inputId = "cities", choices = choices_cities()$registrant_city)  })}

A working example :

library("dplyr")library("shiny")data("world.cities", package = "maps")ui <- fluidPage(  sliderInput(inputId = "n", label = "n", min = 10, max = 30, value = 10),  selectInput(inputId = "cities", label = "Select City", choices = NULL))server <- function(input, output, session) {  choices_cities <- reactive({    choices_cities <- world.cities %>%      arrange(desc(pop)) %>%      top_n(n = input$n, wt = pop)   })  observe({    updateSelectInput(session = session, inputId = "cities", choices = choices_cities()$name)  })}shinyApp(ui = ui, server = server)

Viewing latest article 2
Browse Latest Browse All 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>