2017-08-04 45 views
1

我刚刚开始时闪亮的仪表板,需要一些帮助。selectInput和reactive在Pinyly中的条形图

基本上我试图在带有selectInput函数的Shiny Dashboard中构建一个条形图(使用阴谋),该函数可以选择5月或6月的客户数据。 数据的结构是:

实验< - data.frame(May = c(23,32,34,39,75),June = c(56,23,45,16,76),区域= c(“ABD”,“MAG”,“DBU”,“MBSC”,“PSO”))

任何帮助理解如何链接selectInput与Plotly图将不胜感激。

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) 

回答

1

您可以使用get()你的变量plotly转换的东西可以工作。还请注意,您需要执行color = ~areas而不是color = 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) 

希望这有助于! enter image description here

+1

非常感谢弗洛里安!这将工作治疗 - 看起来真棒:) – Pryore

+0

伟大的,很高兴我可以帮助! – Florian