0

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
})
}
birdbox05
  • 17
  • 4
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What exactly did your `updateNumericInput` code look like? – MrFlick Feb 25 '22 at 02:45
  • Apologies, I have just edited my question to include my sample code. Hope this clarifies my question. – birdbox05 Feb 25 '22 at 03:30

1 Answers1

0
observeEvent(input$country,{
  updateNumericInput(
    session, "pop_size", value = dataframe$population[dataframe$Country == input$country]
  )
})
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225