Remove selectInput Variable Once Chosen

1,979 views
Skip to first unread message

Nick S

unread,
Mar 19, 2015, 7:22:23 PM3/19/15
to shiny-...@googlegroups.com
I have the following issue I am struggling to find a solution to:

The core function of my shiny app is as follows:
  • A list of unique variables from an imported data frame in the form of a selectInput drop down list is presented to user
  • A user selects one of the items in the list and then clicks an actionButton
  • The item from the selectInput field is then written to a table and displayed to the user within the app
I would like to solicit ideas/options for then removing the selected item from the selectInput field once the actionButton is clicked so that it no longer is displayed in the drop-down list. This process would then be repeated each time the actionButton is pressed for a new selection from the drop-down list.

Any minimal examples on how to achieve this would be appreciated.

Joe Cheng

unread,
Mar 20, 2015, 12:58:02 PM3/20/15
to Nick S, shiny-...@googlegroups.com
I wonder whether it'd be better to just have selectInput(multiple=TRUE) and use that as-is; it's not possible to choose the same item twice at a given moment.

However, if you want to do literally as you described, this is an example of how you'd do it:

--

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("letters", "Choose letter(s)", LETTERS,
        multiple = TRUE),
      actionButton("choose", "Choose")
    ),
    mainPanel(
      verbatimTextOutput("list")
    )
  )
)

server <- function(input, output, session) {
  # v$choices will represent the values that have been chosen
  v <- reactiveValues(choices = c())
  
  # When the Choose button is clicked, add the current letters to v$choices
  # and update the selector
  observeEvent(input$choose, {
    v$choices <- append(v$choices, input$letters)
    updateSelectInput(session, "letters",
      choices = LETTERS[!(LETTERS %in% v$choices)]
    )
  })
  
  output$list <- renderPrint({
    v$choices
  })
}

shinyApp(ui, server)


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/458b12db-ef37-4801-b89d-0da9b99a228e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nick S

unread,
Mar 24, 2015, 2:18:09 PM3/24/15
to shiny-...@googlegroups.com, n.si...@gmail.com
Thanks for the help Joe. This is exactly what I was looking to do.

I do, however, still have one (seemingly minor) issue. Using your example...I can successfully remove items from my selectInput list once the user clicks on the actionButton. However, when I get to the last item in the list and click the actionButton...that last item still remains in the list and does not go away. This does not seem to happen in your example using letters and I can't seem to track down why this would be the case for my list.

some code snippetts:

  • Part where I filter data.frame (read in from a .csv file) to include only rows based on date range selected by user
    rotor_data_filtered_date <- reactive({
        data = subset(rotor_data_filtered, date_processed >= input$date_range[1] & date_processed <= input$date_range[2])
        return(data)
    })

  • Part where I create list of unique items/batches from filtered data set
    • I reference this in ui.R with "uiOutput("filtered_batch_list")
    output$filtered_batch_list <- renderUI({
        selectInput("batch_list",
                    "Batches Requiring Disposition:",
                    choices = sort(unique(rotor_data_filtered_date()$run_id)),
                    multiple = F)
    })

  • Part where I filter the selectInput list by removing previously chosen items (based on your example code)
v <- reactiveValues(choices = c())

observeEvent(input$update, {
          v$choices = append(v$choices, input$batch_list)
          updateSelectInput(session, "batch_list",
                          choices = rotor_data_filtered_date()$run_id[!(rotor_data_filtered_date()$run_id %in% v$choices)]
                          )
    })

Any additional help is greatly appreciated.

Joe Cheng

unread,
Mar 24, 2015, 4:19:50 PM3/24/15
to Nick S, shiny-...@googlegroups.com, Winston Chang
This appears to be a bug in Shiny. There doesn't seem to be a way to use updateSelectInput to clear the choices. I've got a proposed fix here:

You can try it yourself with:
devtools::install_github("rstudio/shiny@bugfix/clear-select-choices")

and then restarting R.

Reply all
Reply to author
Forward
0 new messages