[英]Warning: Error in : Aesthetics must be either length 1 or the same as the data (1): x, group and colour
I can get this to work without using Shiny but I am getting this error.我可以在不使用 Shiny 的情况下让它工作,但我收到了这个错误。 Basically I am trying to add lines to the graph based on a checkbox.
基本上我正在尝试基于复选框向图形添加线条。 I do this by creating the vector of values using the checkbox input and then filter the dataframe.
为此,我使用复选框输入创建值向量,然后过滤 dataframe。 I am only trying to get
covidplot
to work at the moment.我现在只是想让
covidplot
工作。 This is a MWE.这是一个 MWE。
UI用户界面
# Define UI for random distribution app ----
ui <- fluidPage(
# App title ----
titlePanel("PM-272 Covid Dashboard"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select the columns ----
radioButtons("var", "Plot Type:",
c("Raw" = "raw",
"Rolling Average" = "avg")),
# br() element to introduce extra vertical spacing ----
br(),
checkboxGroupInput("region", "Regions to show:",
c("East Midlands" = "East Midlands",
"East of England" = "East of England",
"London" = "London",
"North East" = "North East",
"North West" = "North West",
"South East" = "South West",
"South West" = "South West",
"West Midlands" = "West Midlands",
"Yorkshire and The Humber" = "Yorkshire and The Humber"))
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Tabset w/ plot, summary, and table ----
tabsetPanel(type = "tabs",
tabPanel("Cases", plotOutput("covidplot")),
tabPanel("Deaths", plotOutput("deathsplot"))
)
)
)
)
Server服务器
library(httr)
library(tidyverse)
url <- 'https://api.coronavirus.data.gov.uk/v2/data?areaType=region&metric=covidOccupiedMVBeds&metric=newCasesByPublishDate&metric=newDeaths28DaysByDeathDate&metric=newDeaths60DaysByDeathDate&metric=newCasesByPublishDateRollingRate&format=json'
response <- httr::GET(url,timeout(10))
json_text <- content(response, "text")
df <- jsonlite::fromJSON(json_text)
df<-df$body
df[,c(2,3,4)] <- lapply(df[,c(2,3,4)], as.factor)
# isolate the table
df$date = as.Date(df$date, "%Y-%m-%d")
df <- df %>% filter(date >= '2020-07-13')
# Define server logic for random distribution app ----
server <- function(input, output) {
# Reactive expression to generate the requested distribution ----
# This is called whenever the inputs change. The output functions
# defined below then use the value computed from this expression
d <- reactive({
var <- switch(input$var,
raw = newCasesByPublishDate,
avg = newCasesByPublishDateRollingRate,
newCasesByPublishDate)
})
# Generate a plot of the data ----
# Also uses the inputs to build the plot label. Note that the
# dependencies on the inputs and the data reactive expression are
# both tracked, and all expressions are called in the sequence
# implied by the dependency graph.
output$covidplot <- renderPlot({
input$region
ycol <- input$var
grp <- input$region
df2 <- df %>% filter(areaName %in% grp)
covidplot <- df2 %>% ggplot(aes(x=date,y=ycol,group=areaName,color=areaName)) +
geom_line() +
geom_point() +
xlab("Date") +
ylab("Cases") +
ggtitle("Covid Cases Over Time By Region")
ggplotly(covidplot)
})
}
Initially, input$region
is null and you have not addressed that situation.最初,
input$region
是 null 并且您没有解决这种情况。 Try this尝试这个
output$covidplot <- renderPlot({
if (is.null(input$region)) return(NULL)
ycol <- input$var
grp <- input$region
df2 <- df %>% filter(areaName %in% grp)
covidplot <- df2 %>% ggplot(aes(x=date,y=ycol,group=areaName,color=areaName)) +
geom_line() +
geom_point() +
xlab("Date") +
ylab("Cases") +
ggtitle("Covid Cases Over Time By Region")
ggplotly(covidplot)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.