0

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:

The client selects date ranges for the data The client selects the team(s) to analyze from a CheckboxInput (choices = Team 1, Team 2) 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")), 
                 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 %in% input$Teaminput)

 if (input$category == "Player") {
           filtered_data1 <- filtered_data1 %>% filter(Playername %in% input$Playernameinput)
   }

 if (input$category == "Overview") {
           filtered_data1 <- filtered_data1
   }


  ggplot(filtered_data1, aes(x= Date, y = Weight)) +
                     geom_point(size=2)
})
}

'''

Any Help would be great!!

Here is the head of DataFrame

Playername Date Team Weight

chr date fct dbl

1 Player 1 2019-11-09 Team 1 94

2 Player 1 2019-11-16 Team 1 96

3 Player 2 2019-11-23 Team 2 95

4 Player 2 2019-11-30 Team 2 95.2

5 Player 1 2019-11-03 Team 1 NA

6 Player 1 2019-12-27 Team 1 NA

  • Thanks for your suggestion Ben! I added this to the code and it does not fix the issue at hand. – Zachary McClean Jan 05 '21 at 19:07
  • Would you be willing to edit your post with `DataFrame` so I can reproduce the problem? Try using `dput(head(DataFrame))` and then copy/paste. – Ben Jan 05 '21 at 19:08
  • Also, if `Teaminput` is a `checkboxGroupInput` you may want `%in%` instead of `==` for your `filter` – Ben Jan 05 '21 at 19:19
  • Hello Ben, I have included the head of the data frame. The data frame has a multitude of different variables and observations, but I simplified it to only the relevant variables. Additionally, this data frame does not include position or age. That is okay, it will be included later on once the basis of the code for playername can be developed. Note: there are NAs in this data frame. They can not be replaced by zeros. Ideally, R will ignore the NAs because missing data in this case has importance. – Zachary McClean Jan 05 '21 at 19:22
  • I have used %in% in the Teaminput filter now. Thank you for the suggestion – Zachary McClean Jan 05 '21 at 19:25
  • You need a capital "C" in `Category` for `input$Category` in your `if` statements... – Ben Jan 05 '21 at 19:27
  • 1
    Ben, Thank you so much. It is so ridiculous that all the issue was is simply a capital letter! I have poured over that code for days trying to find the issue and it was as simple as that. Thank you so much for your help! – Zachary McClean Jan 05 '21 at 19:59
  • Ben, I don't want to take up too much of your time. You have fixed the problem I asked for. However, would you have any insight regarding why I get the following error with some player inputs? "Warning in `==.default`(Team, input$Teaminput) : longer object length is not a multiple of shorter object length Warning in is.na(e1) | is.na(e2) : longer object length is not a multiple of shorter object length" – Zachary McClean Jan 05 '21 at 20:14
  • Are you using `%in%` instead of `==` when you can have multiple input values? That's the first thing that comes to mind. Otherwise, maybe you can edit your post with something I can reproduce - that's a lot faster for me to provide help. – Ben Jan 05 '21 at 20:23

0 Answers0