0

I'm in the midst of building a Shiny application that shows COVID-19 statistics. Right now, I'm trying to set the default selectInput; selected == "World", with data$location, but for some reason it's setting itself to the first location in the data frame, being "Afghanistan".

                         selectInput(
                       "country",
                       "Country:",
                       choices =
                         data$location,
                       selected =
                         data$location == "World"
                     )

Thank you very much.

3 Answers3

0

Could you try selected = "World" instead of selected = data$location == "World"

Anthony Chau
  • 61
  • 1
  • 3
  • When I change to (selected = "World"), it return no value in the selectInput box, as opposed to the previous (selected = data$location == "World") where it returns "Afghanistan" – Elijah Silva Mar 27 '20 at 18:19
  • Hmm, it's hard to tell why it can't default to "World". I've had similar unexpected behavior with the selectInput as well. Great work on the app though! – Anthony Chau Mar 27 '20 at 18:45
  • I found out how to fix it :) I had to change from data$location to dataToday$location. dataToday being a subset of the data frame that matches X date; X being todays date. Perhaps it couldn't retrieve it from data$location being there were multiple "World" location in the "data" data frame. – Elijah Silva Mar 27 '20 at 18:59
0

Changed data$location to dataToday$location.

dataToday is a subset of the data frame, "data", with only one row of date; that row being todays date.

My thinking is that the selectInput couldn't retrieve "World" from data$location because there were multiple "World" row names. By subsetting the data and only having one row called "World", it was able to retrieve it successfully.

0

In my case, I can't do it in my own main code, when I use selected = NULL, but selection list always show the first list on the data.frame from the list.

but if I wrote now code, with the simple one, i can use selected = NULL, and the selection list will be blank.

Shunya
  • 2,344
  • 4
  • 16
  • 28