Issues creating data filters based on complex UI filtering system (selectInputs dependent on user inputs)

Hello Everyone,

I am creating a Shiny App and have ran into an issue where I get an "Argument is of Length Zero" error when attempting to filter data based on my complex UI filtering system.

The UI filtering system works as follows:

  1. The client selects date ranges for the data
  2. The client selects the team(s) to analyze from a CheckboxInput (choices = Team 1, Team 2)
  3. The client selects a category of analysis from a SelectInput (Choices = Overview, Age, Position, Players)
    a) If the client selects "Overview" nothing should happen (this will show an overview of the data means)
    b) if the client selects "Age" a new SelectInput appears below which allows the client to select an age range for analysis (choices = "less than 20", "20-23", "23-25", "25-30", "more than 30")
    c) if the client selects "Position" a new SelectInput appears below which allows the client to select the position for analysis (choices = Forwards, Defensemen, Goalies)
    d) Lastly, if the client selects "Player" a new SelectInput appears below which allows the client to select a player to analyze their individual data (choices = There are many individual players who could be selected)

In the server, I would like to then create plots based on the filtering system. As you can see in the below code, filtering based on Date and Team is easy. However, when I include some IF statements (note I do not have all the required IF statements just yet) to filter the dynamic filters that change based on the input of the category filter, I receive an "Argument is of Length Zero" error.

My hypothesis is that when the app opens, the category filter is set to "Overview" as a default. Therefore, the Age, Position and Player filters do not yet exist so the server can not find these filters, and displays a "Argument is of Length Zero" error.
Note: if I delete the IF statements, the error is removed (of course the app doesn't do what I want it to but this may provide some insight into the fact that the error is specifically within the IF statements).

Here is the relevant code:

  ui <- dashboardPage(
     fluidRow( 
                       box(title = "Athlete Body Composition",
                            plotOutput("plot1")),
                       box(collapsible =TRUE, 
                            title = "Filters", 
                            dateRangeInput("daterange", "Select Date Range",
                                             start = "2019-01-01",
                                             end = Sys.Date(),
                                             min = "2019-01-01",
                                             max = Sys.Date(),
                                             format = "yyyy/mm/dd",
                                             separator = "-"),
                           checkboxGroupInput("Teaminput", "Select Team", c("Team 1", "Team 2"), selected ="Team 1"),
                           selectInput( "Category", "Select Category", c("Overview", "Position", "Age", "Player")),
                           uiOutput('outputfilter'))
),
)

server <- function(session, input, output) {
 observeEvent(input$Category,{
        categoryfilterinput<- paste(input$Category)
        output$outputfilter <- renderUI({
            switch(categoryfilterinput,
                      "Position" = selectInput("positioninput", "Select Position", choices = c("Forwards","Defencemen", "Goalies")),
                      "Age" = selectInput("ageinput", "Select Age Range", choices = c("less than 20","20-23", "23-25", "25-30", "more than 30")),
                      "Player" = selectInput("Playernameinput", "Select Player", choices = Roster$Playername))
  })    
})

   output$plot1 <- renderPlot({

      filtered_data1 <- filter(DataFrame, Date >= format(input$daterange[1]) & Date <= format(input$daterange[2])) %>% 
                        filter(Team == input$Teaminput)
      
      if (input$category == "Player") {
        filtered_data1 %>% filter(Playername %in% input$Playernameinput)
      }
      
      if (input$category == "Overview") {
        filtered_data1
     }
    
     
      ggplot(filtered_data1, aes(x= Date, y = Weight)) +
                             geom_point(size=2)
      
})
}

Any Help would be great!!

This is not a reprex as it contains private objects i.e. mergedTrainingandNHL_Table

1 Like

Thank you! Sorry about that, I must have missed that when copying the code from R to this forum. I appreciate the feedback

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.