2

I have a Shiny app based on a dataframe that looks like this:

  ID       Date      result
  1      1/1/2010    100
  1      12/12/2010  200
  2      1/1/2011    300
  2      1/1/2011    400

Notice the double ID's...and for ID 2, two dates are the same.

When running, the app looks like this:

enter image description here

But the app gets confused when it is forced to consider ID #2, which has multiple of the same Dates:

enter image description here

The dropdown menu has a blank first choice, but the second choice is populated correct.

How could I correct this, so that the dropdown is populated with any number of identical dates?

(The more I think about this, it is feeling like a bug as opposed to simply wrangling functionality out of the object. It isn't hard to think of numerous situations where duplicate values would be of interest.)

Thanks for your attention.

app.R

library('shiny')

DF <- data.frame(ID=c(1,1,2,2), Date=c('1/1/2010', '12/12/2010', '1/1/2011', '1/1/2011'), result=c(100, 200, 300, 400))
DF$Date <- as.character(DF$Date)

  server <- function(input, output, session) {

  get_id <- reactive({
    id <- DF[which(DF$ID == input$ID), ]
    return(id)})

  output$result <- renderText({ 
    ans <- get_id()
    print(ans)
    paste("Result: ", ans$result)})


  output$dates<-renderUI({
    print(get_id()$Date)
    selectInput('dates', 'Select date:', choices=get_id()$Date, selected=get_id()$Date[1])})
}

ui <- fluidPage(
  fluidRow(column(2,
      numericInput(inputId="ID", label="Pick an ID: ", value=1))),
           column(2, 
      uiOutput("dates")),
          column(3, mainPanel(textOutput("result")))
)

shinyApp(ui = ui, server = server)
tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149

2 Answers2

3

I am not sure why it isn't showing, but there is a get-around for your situation. Use the parameter selectize = FALSE in your selectInput() function. This should give you the desired functionality.

selectInput('dates', 'Select date:', choices=get_id()$Date,selected=get_id()$Date, selectize = FALSE)})
Shiva
  • 789
  • 6
  • 15
  • Amazing, simple little fix works. The documentation merely states `selectize: Whether to use selectize.js or not.` I don't think I would have figured this out without your help. Thanks! – tumultous_rooster Jul 14 '15 at 16:13
0

It looks like selectize recently added support to allow duplicate values (see github issue https://github.com/brianreavis/selectize.js/issues/129)

But it looks like shiny hasn't picked up that version yet (github issue https://github.com/rstudio/shiny/issues/518)

DeanAttali
  • 25,268
  • 10
  • 92
  • 118
  • So I think what this mean, is that Shiny supports having the duplicate values displayed with respect to choices (eg, `A`, `B`, `B` are valid options to choose for the `selectInput`), but the outputs can't be sent through (ie, Shiny can't handle duplicates of the selection/results). – tumultous_rooster Jul 14 '15 at 17:36