[英]Shiny dashboard - reactive bar plot with values from checkbox
根据我的数据,我可以准备一个简单的闪避条 plot
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")
我想把这个 plot 放在 Shiny 仪表板中,连同一个复选框到 select 年。 我愿意
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)
我收到两种类型的错误:
任何人都可以帮忙吗?
这可以像这样实现:
reactive
.fill
year
library(shiny)
library(shinydashboard)
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))
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) {
dat <- reactive({
filter(geo, year %in% input$checkGroup)
})
output$plot1 <- renderPlot({
ggplot(dat(), aes(fill=as.factor(year), x=geo, y=sales))+
geom_bar(position="dodge", stat = "identity")
})
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.