【发布时间】:2021-04-11 17:51:33
【问题描述】:
根据我的数据,我可以准备一个简单的闪避柱图
geo <- data.frame("year" = c(2018, 2018, 2018, 2019, 2019, 2019, 2020, 2020, 2020),
"geo" = c("Europe", "Asia", "America", "Europe", "Asia", "America", "Europe", "Asia", "America"),
"sales" = c(100, 150, 200, 500, 500, 500, 1200, 1800, 1200))
ggplot(geo, aes(fill=as.factor(year), x=geo, y=sales))+
geom_bar(position="dodge", stat = "identity")
我想将此图放在 Shiny 仪表板中,并带有一个用于选择年份的复选框。我愿意
library(shiny)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
# Boxes need to be put in a row (or column)
fluidRow(
box(plotOutput("plot1", height = 250)),
box(
checkboxGroupInput("checkGroup", label = "Year",
choices = list("2018"=2018, "2019" = 2019, "2020" = 2020),
selected = 2018)
)
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(geo, aes(fill=as.factor(input$checkGroup), x=geo, y=sales))+
geom_bar(position="dodge", stat = "identity")
})
}
shinyApp(ui, server)
我收到两种类型的错误:
- 图上的值不会根据复选框的选择而改变 - 每年的图都是一样的(而且值是错误的)
- 我无法生成选择超过一年的情节。我收到以下错误:“错误:美学必须是长度 1 或与数据 (9) 相同:填充”
谁能帮忙?
【问题讨论】:
标签: r shiny shinydashboard