0

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.

1
  • Sp=='input.species' & Country=='input.country'? note the ampersand. Commented Feb 8, 2014 at 20:43

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

3 Comments

Hello Vincent, Thank you very much! Actually it worked for the country list, but it is not working for the RMU list. Any idea why can it be?
I think the code should probably be (i.e., replace the , by a &): subset(NestingArea2, Sp==input$species & Country==input$country)$RMU
Good morning, I had tried that, but it didn't work (maybe I did smthg wrong...). It works perfectly, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.