将 ggplot 与 r-shiny 一起使用时出错(警告:错误:`filter()` 输入`..1` 出现问题。)
Getting error while using ggplot with r-shiny (Warning: Error in : Problem with `filter()` input `..1`.)
我正在尝试开发我的第一个 R shiny 应用程序。我正在尝试创建一个 checkboxGroupInput()。在我的数据集中,有两种不同类型的飞机。因此用户可以决定是要查看两种机型的线图还是只查看其中一种
我 运行 的错误是:
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`.
我的UI代码如下:
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")
)
)
)
)
)
我的服务器代码是:
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))
})
}
感谢您的帮助!
你需要sum
d <- reactive({
filtered <-
aircrash %>%
filter(sum(aircrash %in% input$aircrafttypeInput))
})
我正在尝试开发我的第一个 R shiny 应用程序。我正在尝试创建一个 checkboxGroupInput()。在我的数据集中,有两种不同类型的飞机。因此用户可以决定是要查看两种机型的线图还是只查看其中一种
我 运行 的错误是:
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`.
我的UI代码如下:
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")
)
)
)
)
)
我的服务器代码是:
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))
})
}
感谢您的帮助!
你需要sum
d <- reactive({
filtered <-
aircrash %>%
filter(sum(aircrash %in% input$aircrafttypeInput))
})