【问题标题】:Reactive bar chart in shiny R闪亮R中的反应性条形图
【发布时间】:2019-10-12 02:10:39
【问题描述】:

我正在尝试在闪亮的应用程序中构建一个反应式条形图。我有一个名为 stats 的小表

Team      wins  loss  draws
Arsenal   533   120   256
Chelsea   489   201   153
Liverpool 584   186   246

我想建立一个条形图,显示基于所选球队的胜负和平局。我无法为此创建反应式条形图。有人可以为此建议一个代码或指导我正确的方向吗?

【问题讨论】:

    标签: r shiny bar-chart reactive


    【解决方案1】:

    我们开始吧:

    library(highcharter)
    library(shiny)
    library(dplyr)
    
    df = data.frame(
        team = c("Arsenal", "Chelsea", "Liverpool"),
        wins = c(533, 489, 584),
        loss = c(120, 201, 186),
        draws = c(156, 153, 246)
    )
    
    # Define UI for application that draws a histogram
    ui <- fluidPage(
    
        # Application title
        titlePanel("Football Analysis"),
    
        sidebarLayout(
            sidebarPanel(
                selectizeInput("teams", "Teams", choices = unique(df$team), selected = unique(df$team)[1])
            ),
    
            mainPanel(
               highchartOutput("plot")
            )
        )
    )
    
    server <- function(input, output) {
    
        reactivedf <- reactive({
            filtereddf <- df %>%
                dplyr::filter(team == input$teams)
            filtereddf
        })
    
        output$plot <- renderHighchart({
            highchart() %>%
                hc_add_series(type = "column", reactivedf()$wins, name = "wins") %>%
                hc_add_series(type = "column", reactivedf()$loss, name = "loss") %>%
                hc_add_series(type = "column", reactivedf()$draws, name = "draws") %>%
                hc_xAxis(labels = list(enabled = FALSE)) %>%
                hc_title(text = input$teams)
        })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2021-02-16
      • 2016-03-25
      • 1970-01-01
      • 2021-04-11
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      相关资源
      最近更新 更多