在Shiny中的selectInput和reactive Plotly条形图

时间:2017-08-04 06:17:29

标签: r shiny plotly shinydashboard reactive

我刚刚开始使用闪亮的仪表板并需要一些帮助。

基本上我正在尝试使用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”))

任何有关如何将selectInput与Plotly图表链接起来的帮助都将非常感激。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<div class="container">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Firstname</th>
<th>Email</th>
<th>Lastname</th>
<th>Password</th>
<th>Phone</th>
<th>Created By</th>
<th>Registration Date</th>
<th>Action</th>


</tr>
</thead>
<tbody>

<tr>
<td>John</td>
<td>Doe</td>
<td>john@example.com</td>
<td>Password</td>
<td>Phone</td>
<td>Created By</td>
<td>Registration Date</td>
<td>
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<i class="fa fa-pencil fa-lg"></i>
<span class="caret"></span>
</a>
<ul class="dropdown-menu menuScroll">
<li><a href="">Details</a></li>
<li><a href="">Document</a></li>
<li><a href="">Care Plan</a></li>
<li><a href="">Health Ins Info</a></li>
<li><a href="">Workers Comp Info</a></li>
<li><a href="">Personal Injury Info</a></li>
<li><a href="">Work Related Injuries</a></li>
<li><a href="">Motor Vehicle Accident</a></li>
<li><a href="">Medical History</a></li>
<li><a href="">Past Appointment</a></li>
<li><a href="">Upcoming Appointment</a></li>
<li><a href="">Authorization</a></li>

<li><a href="">Child Patient</a></li>
<li><a href="">Billing Record</a></li>
</ul>
&nbsp;&nbsp;&nbsp;
<a href="/admin/DeletePatient?ID=6476" class="fa fa-trash-o btnDelete" data-toggle="confirmation" style="color:red;" title="" data-original-title="click yes to delete"></a>&nbsp;&nbsp;&nbsp;

</div>
</td>
</tr>

<tr>
<td>John</td>
<td>Doe</td>
<td>john@example.com</td>
<td>Password</td>
<td>Phone</td>
<td>Created By</td>
<td>Registration Date</td>
<td>
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<i class="fa fa-pencil fa-lg"></i>
<span class="caret"></span>
</a>
<ul class="dropdown-menu menuScroll">
<li><a href="">Details</a></li>
<li><a href="">Document</a></li>
<li><a href="">Care Plan</a></li>
<li><a href="">Health Ins Info</a></li>
<li><a href="">Workers Comp Info</a></li>
<li><a href="">Personal Injury Info</a></li>
<li><a href="">Work Related Injuries</a></li>
<li><a href="">Motor Vehicle Accident</a></li>
<li><a href="">Medical History</a></li>
<li><a href="">Past Appointment</a></li>
<li><a href="">Upcoming Appointment</a></li>
<li><a href="">Authorization</a></li>

<li><a href="">Child Patient</a></li>
<li><a href="">Billing Record</a></li>
</ul>
&nbsp;&nbsp;&nbsp;
<a href="/admin/DeletePatient?ID=6476" class="fa fa-trash-o btnDelete" data-toggle="confirmation" style="color:red;" title="" data-original-title="click yes to delete"></a>&nbsp;&nbsp;&nbsp;

</div>
</td>
</tr>

<tr>
<td>John</td>
<td>Doe</td>
<td>john@example.com</td>
<td>Password</td>
<td>Phone</td>
<td>Created By</td>
<td>Registration Date</td>
<td>
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<i class="fa fa-pencil fa-lg"></i>
<span class="caret"></span>
</a>
<ul class="dropdown-menu menuScroll">
<li><a href="">Details</a></li>
<li><a href="">Document</a></li>
<li><a href="">Care Plan</a></li>
<li><a href="">Health Ins Info</a></li>
<li><a href="">Workers Comp Info</a></li>
<li><a href="">Personal Injury Info</a></li>
<li><a href="">Work Related Injuries</a></li>
<li><a href="">Motor Vehicle Accident</a></li>
<li><a href="">Medical History</a></li>
<li><a href="">Past Appointment</a></li>
<li><a href="">Upcoming Appointment</a></li>
<li><a href="">Authorization</a></li>

<li><a href="">Child Patient</a></li>
<li><a href="">Billing Record</a></li>
</ul>
&nbsp;&nbsp;&nbsp;
<a href="/admin/DeletePatient?ID=6476" class="fa fa-trash-o btnDelete" data-toggle="confirmation" style="color:red;" title="" data-original-title="click yes to delete"></a>&nbsp;&nbsp;&nbsp;

</div>
</td>
</tr>

<tr>
<td>John</td>
<td>Doe</td>
<td>john@example.com</td>
<td>Password</td>
<td>Phone</td>
<td>Created By</td>
<td>Registration Date</td>
<td>
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<i class="fa fa-pencil fa-lg"></i>
<span class="caret"></span>
</a>
<ul class="dropdown-menu menuScroll">
<li><a href="">Details</a></li>
<li><a href="">Document</a></li>
<li><a href="">Care Plan</a></li>
<li><a href="">Health Ins Info</a></li>
<li><a href="">Workers Comp Info</a></li>
<li><a href="">Personal Injury Info</a></li>
<li><a href="">Work Related Injuries</a></li>
<li><a href="">Motor Vehicle Accident</a></li>
<li><a href="">Medical History</a></li>
<li><a href="">Past Appointment</a></li>
<li><a href="">Upcoming Appointment</a></li>
<li><a href="">Authorization</a></li>

<li><a href="">Child Patient</a></li>
<li><a href="">Billing Record</a></li>
</ul>
&nbsp;&nbsp;&nbsp;
<a href="/admin/DeletePatient?ID=6476" class="fa fa-trash-o btnDelete" data-toggle="confirmation" style="color:red;" title="" data-original-title="click yes to delete"></a>&nbsp;&nbsp;&nbsp;

</div>
</td>
</tr>

<tr>
<td>John</td>
<td>Doe</td>
<td>john@example.com</td>
<td>Password</td>
<td>Phone</td>
<td>Created By</td>
<td>Registration Date</td>
<td>
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<i class="fa fa-pencil fa-lg"></i>
<span class="caret"></span>
</a>
<ul class="dropdown-menu menuScroll">
<li><a href="">Details</a></li>
<li><a href="">Document</a></li>
<li><a href="">Care Plan</a></li>
<li><a href="">Health Ins Info</a></li>
<li><a href="">Workers Comp Info</a></li>
<li><a href="">Personal Injury Info</a></li>
<li><a href="">Work Related Injuries</a></li>
<li><a href="">Motor Vehicle Accident</a></li>
<li><a href="">Medical History</a></li>
<li><a href="">Past Appointment</a></li>
<li><a href="">Upcoming Appointment</a></li>
<li><a href="">Authorization</a></li>

<li><a href="">Child Patient</a></li>
<li><a href="">Billing Record</a></li>
</ul>
&nbsp;&nbsp;&nbsp;
<a href="/admin/DeletePatient?ID=6476" class="fa fa-trash-o btnDelete" data-toggle="confirmation" style="color:red;" title="" data-original-title="click yes to delete"></a>&nbsp;&nbsp;&nbsp;

</div>
</td>
</tr>



</tbody>
</table>
</div>
</div>

))

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)
          })

1 个答案:

答案 0 :(得分:3)

您可以使用get()将变量转换为可以使用的变量。另请注意,您需要color = ~areas而不是color = areas,否则将尝试使用不存在的对象区域#39;一个工作的例子:

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