I have a shiny app where the first input in the UI is selecting a country from a dropdown list. I then want other numeric inputs (i.e. population size) to update depending on what country is selected. The data is stored as a dataframe, with column one listing the countries, and column two listing the population size for each country. The inputs should retrieve this data from the dataframe. I have tried using updateNumericInput, however my input field for population size in the app is appearing blank and failing to fill with the right value. How would I approach this?
This is a shorter sample of my code:
ui <- dashboardPage(
dashboardHeader(title = "Shiny app"),
dashboardSidebar(
selectInput("country", "Country", choices = dataframe$country_name),
numericInput("pop_size", "Population size", value = 0))
server <- function(input, output, session) {
observeEvent(input$country,{
updateNumericInput(session, "pop_size", value = dataframe$population)
})
}
I have also tried this for the server instead, but still no luck:
server <- function(input, output, session) {
reactive({
#filter dataframe to selected country
data <- dataframe
data <- data[data$country_name == input$country,]
updateNumericInput(session, "pop_size", value = data$population)
data
})
}