huangli
huangli

Reputation: 454

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

I am new to shiny and have a problem about the slider input, it works well for the dataset but not working for my histogram, could you please help me to look at it, thanks.

Overview

I am trying to build a shiny application to display the attitude{datasets}, the first tab just displays the data, the slider works pretty well, but in the second tab the slider input not works for my histogram. I don't know why, I tried rChart before it also works. Please ignore the about.md file, it's just description.

Code

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

My Question

Why the slider not working for my ggplot histogram. But works for the dataset ?Thanks a lot.

Upvotes: 0

Views: 972

Answers (1)

Pork Chop
Pork Chop

Reputation: 29407

Try this

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

Upvotes: 1

Related Questions