I often use the updateSelectInput
function in shiny to filter the choices in a select input whenever the selection of another related input changes. The code would look something like this:
observeEvent(input$filter1, {
filter2_select <- input$filter2
filter2_options <- data %>%
filter(Var1 == input$filter1) %>%
pull(Var2) %>%
unique()
if(filter2_select %in% filter2_options) {
updateSelectInput(session, "filter2", choices = filter2_options, selected = filter2_select)
} else {
updateSelectInput(session, "filter2", choices = filter2_options)
}
})
This is useful to reduce the combination of filter options available to the user to only those for which there are data in a data set.
The resulting inputs are then used to filter the data which is then passed to a plot to visualise.
However I can't seem to get around the problem of getting the following error message appearing during the time input$filter2
is 'updating':
Warning: Error in $<-.data.frame: replacement has 1 row, data has 0
It will normally only appear for a fraction of a second which I'm assuming is the time the value in the updating input reverts to NULL
then disappears when it has successfully updated it's selection and a new plot is rendered.
Is there away to avoid this and only have the plot re-render when the input has a valid selection and the data can be filtered without an error?
I've tried using req()
and validate(need(...))
but they only solve the problem of the removing the error message, not the problem of the plot disappearing and then reappearing, however briefly.
Thanks,
Paul