Deepansh Arora
Deepansh Arora

Reputation: 742

Getting error while using ggplot with r-shiny (Warning: Error in : Problem with `filter()` input `..1`.)

I'm trying to develop my first R shiny app. I'm trying to create a checkboxGroupInput(). In my dataset there are two different types of aircraft. So user can decide if he wants to see line plot of both the aircraft types or only one of them

The error that I'm running is:

Warning: Error in : Problem with `filter()` input `..1`.
x Input `..1` must be of size 5514 or 1, not size 25.
i Input `..1` is `aircrash %in% input$aircrafttypeInput`.

My UI code is as follows:


library(shiny)
library(shinythemes)
library(dplyr)
library(ggplot2)


aircrash <-read.csv("aircrash_clean_data.csv", header = T)

# Define UI for application that draws a histogram
ui <- fluidPage(
     navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                           "Aircrash Investigation Analysis", id="nav",
                tabPanel("Crashes",
                         sidebarLayout(
                             sidebarPanel(
    sliderInput("bin", "Please select the bin width:",
                min=0, max=20, value=4, step=1),
    checkboxGroupInput("aircrafttypeInput", "Select the Aircraft Type:",
                       choices = c("Civilian",
                                   "Military"),
                       selected = c("Civilian", "Military"))
    
                                        
),
mainPanel(
    plotOutput("plot", width = "75%",height="530px"),
    plotOutput("Aircrafttypecount")
)
)
)
)    
)

My server code is:

server <- function(input, output) {

    output$plot <- renderPlot({
        # generate bins based on input$bins from ui.R
        ggplot(data=aircrash, aes(x= crash_year))+
            geom_histogram(binwidth = as.numeric(input$bin))
    })
    
    d <- reactive({
        filtered <-
            aircrash %>%
            filter(aircrash %in% input$aircrafttypeInput)   
        
    }) 
    
    output$Aircrafttypecount <-renderPlot({
        ggplot(data=d(), aes(x= crash_year, y=stat(count), color =crash_opr_type ))+
            geom_line(stat = "count", size = 1.5)+theme_bw()+
            theme(plot.title = element_text(color = "black", size = 20, face = "bold", hjust = 0.5), 
                  axis.title.x =element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
                  axis.title.y = element_text(color = "black", size = 14, face = "bold", hjust = 0.5))+
            labs(y = "Number of Aircrashes",x="Year",title = "Number of Crashes per Year",color = "Aircraft Type")+
            expand_limits(y=c(0,100)) + 
            scale_y_continuous(breaks=seq(0, 100, 20))+coord_cartesian(xlim=c(1908, 2028)) + 
            scale_x_continuous(breaks=seq(1908, 2028, 10))
    })
    
}

Thanks for the help!

Upvotes: 1

Views: 1334

Answers (1)

HubertL
HubertL

Reputation: 19544

You need to sum

d <- reactive({
        filtered <-
            aircrash %>%
            filter(sum(aircrash %in% input$aircrafttypeInput))  
        
    }) 

Upvotes: 3

Related Questions