1
votes

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.

library(shiny)
library(tidyverse)


entity <- c('A', 'B', 'C', 'D', 'E')
ge <- data.frame(entity, replicate(16,sample(0:100,5,rep=TRUE)))
names(ge) <- c('entity', 
               'first_u7_2011','first_u7_2012','first_u7_2013','first_u7_2014',
               'first_79_2011','first_79_2012','first_79_2013','first_79_2014',
               'second_u7_2013','second_u7_2014',
               'second_79_2013','second_79_2014',
               'third_u7_2013','third_u7_2014',
               'third_79_2015','third_79_2016')

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(
  titlePanel(""),
  sidebarLayout(position = "left",
                sidebarPanel(
                  selectInput("Gruppe", label = "Gruppe:", Gruppe),
                  selectInput("Kategorie", label = "Kategorie:", Kategorie),
                  uiOutput("Slider")),
                mainPanel("Tabelle", tableOutput("tableview"))))


#Server Seite
server <- function(input, output, session) {


  gruppe <- reactive({
    switch(input$Gruppe,
           "Unter 7" = select(ge, entity, matches("u7")),
           "7-9 Jahre" = select(ge, entity, matches("79")))
  })

  kategorie <- reactive({
    switch(input$Kategorie,
           "first" = select(gruppe(), entity, matches("first")),
           "second" = select(gruppe(), entity, matches("second")),
           "third" = select(gruppe(), entity, matches("third")))
  })

  zeitraum1 <- reactive({
    switch(input$Gruppe,
           "Unter 7" = zeitraum.list[c(1,3,5)],
           "7-9 Jahre" = zeitraum.list[c(2,4,6)])
  })

  zeitraum2 <- reactive({
    switch(input$Kategorie,
           "first" = zeitraum1()[[1]],
           "second" = zeitraum1()[[2]],
           "third" = zeitraum1()[[3]])
  })




  output$Slider <- renderUI({
    if (is.null(input$Kategorie))
      return()
    switch(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"))}
    return(filtered) 
  })


  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!

1

1 Answers

0
votes

your problem can be solved quit easily with the use of req.

your problem comes when you want to access more columns from the data-fram than there actually are. Therefore just add an req statement checking that the data frame has atleast 2 columns and everything works fine

output$tableview <- renderTable({
    req(ncol(filtered()) > 1)
    table <- filtered()[, c(1,2)]
    head(table[, c(1,2)], n = 5)
  })

Hope this helps!!