【问题标题】:Shiny dashboard - reactive bar plot with values from checkbox闪亮的仪表板 - 带有复选框值的反应性条形图
【发布时间】: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. 图上的值不会根据复选框的选择而改变 - 每年的图都是一样的(而且值是错误的)
  2. 我无法生成选择超过一年的情节。我收到以下错误:“错误:美学必须是长度 1 或与数据 (9) 相同:填充”

谁能帮忙?

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    这可以这样实现:

    1. 您必须根据检查的年份过滤数据集。为此,请使用reactive
    2. 在 renderPlot 中使用响应式返回的数据帧进行绘图,并简单地将 year 映射到 fill
    
    
        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)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 2020-06-10
      • 2021-02-16
      • 2019-10-12
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      相关资源
      最近更新 更多