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

Shiny slider input filter not working for ggplot but works for dataset

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

概览

我正在尝试构建一个闪亮的应用程序来显示态度{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 直方图。但适用于数据集?非常感谢。

试试这个

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