3
votes

My Shiny app uses open data from a bird atlas, including lat/lon coordinates by species. The bird species names come in different languages, plus as an acronym.

The idea is that the user first selects the language (or acronym). Based on the selection, Shiny renders a selectizeInput list of unique bird species names. Then, when one species is selected, a leaflet map is generated.

I've done a couple of Shiny apps but this time I miss something obvious. When the app starts, all is well. But, the selectizeInput list is not re-rendered when a new language is selected.

All the present code with some sample data is here as a GitHub Gist https://gist.github.com/tts/924b764e7607db5d0a57

If somebody could point to my problem, I'd be grateful.

1

1 Answers

6
votes

The problem is that both the renderUI and the birds reactive blocks are dependent on the input$lan input.

If you add print(input$birds) in the birds block, you will see that it uses the name of the birds before renderUI has a chance to update them to fit the new language. The data you then pass the leaflet plot is empty.

Try adding isolate around input$lan in the birds expressions so that it is only dependent on input$birds:

birds <- reactive({
    if( is.null(input$birds) )
      return()
    data[data[[isolate(input$lan)]] == input$birds, c("lon", "lat", "color")]
  })

When you change the language, the renderUI will change the selectize, which will trigger the input$birds and update the data.

Instead of using renderUI, you could also create the selectizeInput in your ui.R using (replacing the uiOutput):

selectizeInput(
        inputId = "birds", 
        label = "Select species",
        multiple  = F,
        choices = unique(data[["englanti"]])
      )

And in your server.R, update it using:

observe({
    updateSelectizeInput(session, 'birds', choices = unique(data[[input$lan]]))
  })