Question

I want to make a list for a selectInput from a CSV file, but from a subset made based on two previous selectInputs. This means that on my app:

  1. the user chooses a species name from a list

    radioButtons("species", "Which species are you workingwith?", list("Caretta caretta"="Cc", "Chelonia mydas"="Cm", "Dermochelys coriacea"="Dc", "Eretmochelys imbricata"="Ei", "Lepidochelys kempii"="Lk", "Lepidochelys olivacea"="Lo", "Natator depressus"="Nd"))

  2. the user chooses a nesting area (country) from a list based on the species:

    conditionalPanel( condition="input.country_type=='List' & input.species=='Cc'", selectInput("country", "Country:", choices=subset(NestingArea2, Sp=='Cc')$Country)),

           conditionalPanel(
             condition="input.country_type=='List' & input.species=='Cm'",
             selectInput("country", "Country:",
                         choices=subset(NestingArea2, Sp=='Cm')$Country)),
           ......
    
  3. and then the user must choose a RMU from a list, which is different for each "species" and "country". I have tried this and it didn't work:

    selectInput("rmu", "RMU:", choices=subset( NestingArea2, Sp=='input.species', Country=='input.country')$RMU)

The .csv (NestingArea2) file has 3 columns as follows: Sp | Country | RMU

I could do what I've done on (2), but since there are many countries, I am searching for something easier.

Was it helpful?

Solution

Creating a conditionalPanel and selectInput for each country|RMU separately will be very tedious and (coding) error prone. What you are looking for is a dynamic UI where the choices in a selectInput depend on previous choices.

I haven't tested this because I don't have your data but the following should get you most of the way there. Put the two outputs below in server.R. Then put the uiOutputs in ui.R (note: add comma's as needed). Before even doing that however, make sure to read the Shiny documentation on dynamic ui linked above.

Put in server.R

output$countrySelect <- renderUI({
  countryChoices <- subset(NestingArea2, Sp==input$species)$Country)
  selectInput("country", "Country:", choices=countryChoices)
})

output$rmuSelect <- renderUI({
  rmuChoices <- subset(NestingArea2, Sp==input$species, Country==input$country)$RMU
  selectInput("rmu", "RMU:", choices=rmuChoices)
})

Put in ui.R

uiOutput('countrySelect'),
uiOutput('rmuSelect')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top