RShiny-警告:错误:美学必须长度为1或与数据相同(48):y

时间:2019-02-25 22:47:49

标签: r ggplot2 shiny

我正在尝试让输入函数链接到脚本,但是我一直遇到错误。我正在尝试添加一个复选框选项,以过滤我在图中的数据。

我曾尝试在许多不同的地方将输入函数放置在脚本(input$rating)中,但仍然收到类似的错误。我到处都是,但是似乎找不到解决该问题的常用答案。

ui <- fluidPage("Retired Risks by Retired Date",
                checkboxGroupInput(inputId = "rating", label = "Risk Rating", 
                                   choices = c("Critical","Major","Moderate","Minor")),
                plotOutput("plot1"))

server <- function(input, output){
  Risk <- read.csv("C:/Users/ABakk1/Desktop/RShinyTest.csv")
  df <- data.frame(Risk$Risk_Retired_Date, Risk$Risk_Rating)
  dfsum <- df %>%
    group_by(Risk.Risk_Retired_Date, Risk.Risk_Rating) %>%
    tally()
  dfsum$Risk.Risk_Retired_Date <- factor(dfsum$Risk.Risk_Retired_Date, 
                                         levels=c("18-Jan","18-Feb","18-Mar","18-Apr",
                                                  "18-May","18-Jun","18-Jul","18-Aug",
                                                  "18-Sep","18-Oct","18-Nov","18-Dec",
                                                  "19-Jan","19-Feb"))

  output$plot1 <- renderPlot({
    print(ggplot(dfsum, aes(x = Risk.Risk_Retired_Date, 
                            y = n<input$rating, colour = Risk.Risk_Rating ))) + 
    geom_line(aes(group = Risk.Risk_Rating), size = 1.5) + 
    scale_color_manual(name = "Risk Rating", 
                       labels = c("Critical", "Major", "Minor", "Moderate"),
          values = c("Critical"="darkred", "Major"="orangered", 
                     "Moderate"="darkorange", "Minor"="darkgoldenrod1")) + 
    scale_y_continuous(limits = c(0,35)) +
    theme_bw() +
    theme(panel.grid.major = element_blank(), 
          legend.key=element_blank(), legend.justification = c(0,1),
          legend.position = c(0.05,0.95), 
          legend.box.background = element_rect(colour = "black"), 
          text=element_text(size=14,  family="Arial Narrow")) +
    labs(title = "Retired Risks by Date", 
         y = "Number Risks Closed", x = "Risk Retired Date") +
    guides(color=guide_legend(override.aes=list(fill=NA)))})
}

shinyApp(ui, server)

这是我的身影:Line Graph

我只想添加过滤器选项。

据我所知,我认为我没有将input$rating放在正确的位置,但是我看到了许多不同的脚本将其放置在不同的位置。这个问题容易解决吗?

1 个答案:

答案 0 :(得分:0)

input $ rating中存储的值是以下一个或多个字符的向量:c("Critical","Major","Moderate","Minor")

在renderPlot函数内的ggplot调用中,您正在评估input $ rating,就好像它是一个数字一样:

print(ggplot(dfsum, aes(x = Risk.Risk_Retired_Date, 
                            y = n<input$rating, colour = Risk.Risk_Rating ))) + 

相反,我将创建一个反应性对象,以在求和之前存储新的数据帧,并按您的选择进行过滤:

dfsum <- reactive({
df %>%
   # New code below
   filter(Risk.Risk_Rating %in% input$rating) %>%

   # Your original code
   group_by(Risk.Risk_Retired_Date, Risk.Risk_Rating) %>%
   tally()
   dfsum$Risk.Risk_Retired_Date <- factor(dfsum$Risk.Risk_Retired_Date, 
                                         levels=c("18-Jan","18-Feb","18-Mar","18-Apr",
                                                  "18-May","18-Jun","18-Jul","18-Aug",
                                                  "18-Sep","18-Oct","18-Nov","18-Dec",
                                                  "19-Jan","19-Feb"))
})
``