闪亮的滑块输入过滤器不适用于ggplot但适用于数据集

时间:2015-11-13 07:07:48

标签: r ggplot2 shiny histogram

我是新手,有滑块输入的问题,它适用于数据集,但不适用于我的直方图,请你帮我看一下,谢谢。

概述

我正在尝试构建一个闪亮的应用程序来显示姿态{datasets},第一个选项卡只显示数据,滑块工作得很好,但在第二个选项卡中,滑块输入不适用于我的直方图。我不知道为什么,我尝试过rChart之前它也有效。请忽略about.md文件,它只是描述。

代码

ui.r

library(shiny)
require(markdown)
library(ggplot2)

# Define UI for application that draws a histogram
shinyUI(

  navbarPage("Employee attitude survey", 
             # multi-page user-interface that includes a navigation bar.
             tabPanel("Explore the Data",
                      sidebarPanel(
                        sliderInput("rating",
                                    "Employee rating filter:",
                                    min = 1,
                                    max = 100,
                                    value = c(10,50))
                      ),
                      # Show a plot of the generated distribution  
                      # mytable1: dataset 
                      # distPlot: histogram
                      mainPanel(
                        tabsetPanel(
                          tabPanel(p(icon("table"), "Dataset"),
                                   dataTableOutput("mytable1")),
                          tabPanel(p(icon("search"), "Visualize the Data"),
                                   plotOutput("distPlot"))
                        ) 
                      )
             ),
             tabPanel("About",
                      mainPanel(
                        includeMarkdown("about.md")
                      )
             ) # end of "About" tab panel
  )
)

server.R

library(shiny)
library(ggplot2)

# Define server logic required to draw a histogram and a table
shinyServer(function(input, output) {

  # table to display the attitude, slider works
  output$mytable1 = renderDataTable({
    attitude[which(attitude$rating <= input$rating[2] &  attitude$rating >= input$rating[1]), ]
  })

  # histogram of rating, but slider not works
  output$distPlot <- renderPlot({
    df <- attitude[which(attitude$rating <= input$rating[2] &  attitude$rating >= input$rating[1]), ]
    p1 <- ggplot() + aes(df[,"rating"])
    p1 <- p1 + geom_histogram(binwidth=2, col="skyblue", aes(fill=..count..), alpha=0.6) 
    p1
  })
})

我的问题

为什么滑块不能用于我的ggplot直方图。但适用于数据集?非常感谢。

1 个答案:

答案 0 :(得分:1)

试试这个

  # histogram of rating, but slider not works
  output$distPlot <- renderPlot({
    df <- attitude[which(attitude$rating <= input$rating[2] &  attitude$rating >= input$rating[1]), ]
    test <<- (df[,"rating"])
    p1 <- ggplot() + aes(test)
    p1 <- p1 + geom_histogram(binwidth=2, col="skyblue", aes(fill=..count..), alpha=0.6) 
    p1
  })