'Reactive bar chart in shiny R

I am trying build a reactive bar chart in shiny app. I have a small table named stats

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

I want to build a bar chart which displays the wins, loss and draws based on the team selected. I'm not able to create a reactive bar chart for this. Can someone please suggest a code for this or guide me in a right direction?



Solution 1:[1]

Here we go:

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DSGym