Shiny 中的 selectInput 和反应式 Plotly 条形图
selectInput and reactive Plotly bar chart in Shiny
我刚刚开始使用闪亮的仪表板,需要一些帮助。
基本上,我试图在带有 selectInput 函数的 Shiny Dashboard 中构建一个条形图(使用 plotly),该函数选择 5 月或 6 月的客户数据。
数据结构为:
实验 <- data.frame(5 月 = c(23,32,34,39,75),6 月 = c(56,23,45,16,76),面积 = c("ABD","MAG","DBU","MBSC","PSO"))
任何有助于理解如何 link 使用 Plotly 图表的 selectInput 的帮助将不胜感激。
ui <- dashboardPage(skin = "red",
dashboardHeader(
title = "iReport",
titleWidth = 500
),
dashboardSidebar(
# Code assigning items to left-hand menu pane
sidebarMenu(
menuItem("Dashboard", tabName = "Dashboard", icon = icon("Dashboard")),
menuItem("Survey", tabName = "Survey", icon = icon("Survey")),
menuItem("Enquiries", tabName = "Enquiries", icon = icon("Enquiries")),
menuItem("Metrics", tabName = "Metrics", icon = icon("Metrics")),
menuItem("Metrics", tabName = "Metrics", icon = icon("Metrics"))
)),
dashboardBody(
tabItems(
# Tab for Dashboard
tabItem(tabName = "Dashboard"),
# Tab for Survey Analytics
tabItem(tabName = "Survey"),
#Tab for Enquiry Analytics
tabItem(tabName = "Enquiries"),
#Tab for Web Analytics
tabItem(tabName = "Metrics"),
#Tab for Twitter Analytics
tabItem(tabName = "Twitter")
),
# Row 1 objects
fluidRow(
# Value boxes
valueBox(479, "Total No. of Enquiries", color = "red", icon = icon("commenting")),
valueBox(1.7, "Average response time", color = "red", icon = icon("exchange")),
valueBox(98, "No. of satisfied customers", color = "red", icon = icon("thumbs-up"))
),
# Row 2 objects
fluidRow(
# Data visualisation 2
box(width = 6, plotlyOutput("Test2", height = "600px")),
box(width = 4, selectInput("select", "Select", label = h1("Selection Pane"),choices = list(May = "May", June = "June")))
)
))
server <- function(input, output){
#Data
Experiment <- data.frame(May = c(23,32,34,39,75), June = c(56,23,45,16,76), areas = c("ABD","MAG","DBU","MBSC","PSO"))
# Plotly bar chart
output$Test2 <- renderPlotly({
Test2 <- plot_ly(
Experiment, x = areas, y = input$select, type = "bar", color = areas)
})
}
shinyApp(ui, server)
您可以使用 get()
将您的变量转换为 plotly 可以使用的变量。另请注意,您需要执行 color = ~areas
而不是 color = areas
,否则 plotly 将尝试使用不存在的对象 'areas'。一个工作示例:
library(shiny)
server <- function(input, output){
#Data
Experiment <- data.frame(May = c(23,32,34,39,75), June = c(56,23,45,16,76), areas = c("ABD","MAG","DBU","MBSC","PSO"))
# Plotly bar chart
output$Test2 <- renderPlotly({
Test2 <- plot_ly(
Experiment, x = ~areas, y = ~get(input$select), type = "bar", color = ~areas)
})
}
ui <- shinyUI(fluidPage(
plotlyOutput("Test2", height = "600px"),
selectInput("select", "Select", label = h1("Selection Pane"),choices = list(May = "May", June = "June"))
)
)
shinyApp(ui,server)
希望对您有所帮助!
我刚刚开始使用闪亮的仪表板,需要一些帮助。
基本上,我试图在带有 selectInput 函数的 Shiny Dashboard 中构建一个条形图(使用 plotly),该函数选择 5 月或 6 月的客户数据。 数据结构为:
实验 <- data.frame(5 月 = c(23,32,34,39,75),6 月 = c(56,23,45,16,76),面积 = c("ABD","MAG","DBU","MBSC","PSO"))
任何有助于理解如何 link 使用 Plotly 图表的 selectInput 的帮助将不胜感激。
ui <- dashboardPage(skin = "red",
dashboardHeader(
title = "iReport",
titleWidth = 500
),
dashboardSidebar(
# Code assigning items to left-hand menu pane
sidebarMenu(
menuItem("Dashboard", tabName = "Dashboard", icon = icon("Dashboard")),
menuItem("Survey", tabName = "Survey", icon = icon("Survey")),
menuItem("Enquiries", tabName = "Enquiries", icon = icon("Enquiries")),
menuItem("Metrics", tabName = "Metrics", icon = icon("Metrics")),
menuItem("Metrics", tabName = "Metrics", icon = icon("Metrics"))
)),
dashboardBody(
tabItems(
# Tab for Dashboard
tabItem(tabName = "Dashboard"),
# Tab for Survey Analytics
tabItem(tabName = "Survey"),
#Tab for Enquiry Analytics
tabItem(tabName = "Enquiries"),
#Tab for Web Analytics
tabItem(tabName = "Metrics"),
#Tab for Twitter Analytics
tabItem(tabName = "Twitter")
),
# Row 1 objects
fluidRow(
# Value boxes
valueBox(479, "Total No. of Enquiries", color = "red", icon = icon("commenting")),
valueBox(1.7, "Average response time", color = "red", icon = icon("exchange")),
valueBox(98, "No. of satisfied customers", color = "red", icon = icon("thumbs-up"))
),
# Row 2 objects
fluidRow(
# Data visualisation 2
box(width = 6, plotlyOutput("Test2", height = "600px")),
box(width = 4, selectInput("select", "Select", label = h1("Selection Pane"),choices = list(May = "May", June = "June")))
)
))
server <- function(input, output){
#Data
Experiment <- data.frame(May = c(23,32,34,39,75), June = c(56,23,45,16,76), areas = c("ABD","MAG","DBU","MBSC","PSO"))
# Plotly bar chart
output$Test2 <- renderPlotly({
Test2 <- plot_ly(
Experiment, x = areas, y = input$select, type = "bar", color = areas)
})
}
shinyApp(ui, server)
您可以使用 get()
将您的变量转换为 plotly 可以使用的变量。另请注意,您需要执行 color = ~areas
而不是 color = areas
,否则 plotly 将尝试使用不存在的对象 'areas'。一个工作示例:
library(shiny)
server <- function(input, output){
#Data
Experiment <- data.frame(May = c(23,32,34,39,75), June = c(56,23,45,16,76), areas = c("ABD","MAG","DBU","MBSC","PSO"))
# Plotly bar chart
output$Test2 <- renderPlotly({
Test2 <- plot_ly(
Experiment, x = ~areas, y = ~get(input$select), type = "bar", color = ~areas)
})
}
ui <- shinyUI(fluidPage(
plotlyOutput("Test2", height = "600px"),
selectInput("select", "Select", label = h1("Selection Pane"),choices = list(May = "May", June = "June"))
)
)
shinyApp(ui,server)
希望对您有所帮助!