Problems with reactive object inside an eventReactive expression



Hi everyone,
I receive the error: "Warning: Error in [.data.frame: undefined columns selected" in my Shiny App. For simplicity my reproducible example only renders a table output but in actual use I have a leaflet map object which always uses the selected column as values argument which is produced by renderTable. The goal is to construct via user input a filtered dataframe.

As you can see I am using a renderUI object because I need flexible ranges in my slider widget. Because of this I need an eventReactive object which in the end filters down the dataframe into two columns: 'entity' and the user input value column.


entity <- c('A', 'B', 'C', 'D', 'E')
ge <- data.frame(entity, replicate(16,sample(0:100,5,rep=TRUE)))
names(ge) <- c('entity', 

Gruppe <- c("Unter 7", "7-9 Jahre")
Kategorie <- c("first", "second", "third")

zeitraum.list <- list(jahre_first_u7 = c(2011:2014),
                      jahre_first_79 = c(2011:2014),
                      jahre_second_u7 = c(2013:2014),
                      jahre_second_79 = c(2013:2014),
                      jahre_third_u7 = c(2013:2014),
                      jahre_third_79 = c(2015:2016))

#UI Seite
ui <- fluidPage(
  sidebarLayout(position = "left",
                  selectInput("Gruppe", label = "Gruppe:", Gruppe),
                  selectInput("Kategorie", label = "Kategorie:", Kategorie),
                mainPanel("Tabelle", tableOutput("tableview"))))

#Server Seite
server <- function(input, output, session) {
  gruppe <- reactive({
           "Unter 7" = select(ge, entity, matches("u7")),
           "7-9 Jahre" = select(ge, entity, matches("79")))
  kategorie <- reactive({
           "first" = select(gruppe(), entity, matches("first")),
           "second" = select(gruppe(), entity, matches("second")),
           "third" = select(gruppe(), entity, matches("third")))
  zeitraum1 <- reactive({
           "Unter 7" = zeitraum.list[c(1,3,5)],
           "7-9 Jahre" = zeitraum.list[c(2,4,6)])
  zeitraum2 <- reactive({
           "first" = zeitraum1()[[1]],
           "second" = zeitraum1()[[2]],
           "third" = zeitraum1()[[3]])

  output$Slider <- renderUI({
    if (is.null(input$Kategorie))
           "first" = sliderInput("inSlider", "Zeitleiste:",
                                           min = min(zeitraum2()),
                                           max = max(zeitraum2()),
                                           step = 1,
                                           value = min(zeitraum2()),
                                           sep = ""),
           "second" = sliderInput("inSlider", "Zeitleiste:",
                                            min = min(zeitraum2()),
                                            max = max(zeitraum2()),
                                            step = 1,
                                            value = min(zeitraum2()),
                                            sep = ""),
           "third" = sliderInput("inSlider", "Zeitleiste:",
                                            min = min(zeitraum2()),
                                            max = max(zeitraum2()),
                                            step = 1,
                                            value = min(zeitraum2()),
                                            sep = ""))
  filtered <- eventReactive(req(input$inSlider, kategorie()), {
    if(input$inSlider == "2011"){filtered = select(kategorie(), entity, matches("2011"))}
    if(input$inSlider == "2012"){filtered = select(kategorie(), entity, matches("2012"))}
    if(input$inSlider == "2013"){filtered = select(kategorie(), entity, matches("2013"))}
    if(input$inSlider == "2014"){filtered = select(kategorie(), entity, matches("2014"))}
    if(input$inSlider == "2015"){filtered = select(kategorie(), entity, matches("2015"))}
    if(input$inSlider == "2016"){filtered = select(kategorie(), entity, matches("2016"))}
  output$tableview <- renderTable({
    table <- filtered()[, c(1,2)]
    head(table[, c(1,2)], n = 5)

shinyApp(ui, server)

The error always occurs when the user input changes to a 'Kategorie' or 'Gruppe' input where the slider value of the last slider widget is not exisiting in the new generated slider. For example: If you run the app and just change the 'Kategorie' input to 'third'.
This is extremely problematic with a leaflet object because the app then not only throws the error but as well crashes completely. I am pretty sure the problem is in the eventReactive expression because in the example case just stated, the reactive object does not know how to ignore the possible user input of a time value which does not exist in its slider range.

How can I achieve this? Any help is greatly appreciated!