我正在尝试在闪亮的应用程序中构建反应式条形图。我有一个名为stats的小表
Team wins loss draws
Arsenal 533 120 256
Chelsea 489 201 153
Liverpool 584 186 246
我想构建一个条形图,该条形图根据所选团队显示获胜,失败和平局。我无法为此创建反应式条形图。有人可以为此建议一个代码或指导我正确的方向吗?
答案 0 :(得分:2)
我们在这里:
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)