如何解决闪亮的仪表板 - 带有复选框值的反应条形图
根据我的数据,我可以准备一个简单的闪避条形图
geo <- data.frame("year" = c(2018,2018,2019,2020,2020),"geo" = c("Europe","Asia","America","Europe","America"),"sales" = c(100,150,200,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),y=sales))+
geom_bar(position="dodge",stat = "identity")
})
}
shinyApp(ui,server)
我收到两种类型的错误:
有人可以帮忙吗?
解决方法
这可以像这样实现:
- 您必须根据检查的年份过滤数据集。为此,请使用
reactive
。 - 在renderPlot 中使用reactive 返回的数据框进行绘图并简单地将
year
映射到fill
上
library(shiny)
library(shinydashboard)
geo <- data.frame("year" = c(2018,2018,2019,2020,2020),"geo" = c("Europe","Asia","America","Europe","America"),"sales" = c(100,150,200,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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。