Shiny package filtering graphs on select input

I am trying to create a shiny dashboard that displays a reactive graph based on select input. I want to graph a subset of the data based on what the user selects. The code below generates a graph but only for 1 of the 5 drop downs. Having trouble with filtering, any help would be appreciated.

ui <- fluidPage(
   titlePanel("Dashboard"),
   
   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         selectInput(inputId = "quintile", label = ("technology quintile"),
                     choices = unique(spin_rate$Quintile.Base)
                     )),
   

      

      mainPanel(
         plotOutput(outputId = "plot", height = "500px", width="500px")
      )
   )
)


# Define server logic required to draw a histogram
server <- function(input, output) {
 filtered_data<- reactive({
  dplyr::filter(spin_rate, spin_rate$Quintile.Base==input$quintile)
})
   output$plot <- renderPlot({
      par(mar=c(1,1,1,1))
     
      plot(x= filtered_data()$baseline_spin, y= filtered_data()$Difference.Error, type="p", xlim=c(1400,2000), ylim=c(.00,.8), xlab="Rate", ylab="Difference error")
   })
}
      
      
*emphasized text*
# Run the application 
shinyApp(ui = ui, server = server)

Hi @rstudionubie. Because you didn't supply the table spin_rate, I make a random table. Your code seems okay. So, the problem may due to your data spin_rate.

library(shiny)

spin_rate <- data.frame(baseline_spin = sample(1:10, 100, replace = TRUE),
           Difference.Error = sample(1:10, 100, replace = TRUE),
           Quintile.Base = sample(LETTERS[1:5], 100, replace = TRUE),
           stringsAsFactors = FALSE)


ui <- fluidPage(
  titlePanel("Dashboard"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "quintile", label = ("technology quintile"),
                  choices = unique(spin_rate$Quintile.Base)
      )),
    
    
    
    
    mainPanel(
      plotOutput(outputId = "plot", height = "500px", width="500px")
    )
  )
)


# Define server logic required to draw a histogram
server <- function(input, output) {
  filtered_data<- reactive({
    dplyr::filter(spin_rate, spin_rate$Quintile.Base==input$quintile)
  })
  output$plot <- renderPlot({
    par(mar=c(1,1,1,1))
    
    plot(x= filtered_data()$baseline_spin, y= filtered_data()$Difference.Error, type="p", xlab="Rate", ylab="Difference error")
  })
}


shinyApp(ui = ui, server = server)

hi @raytong that worked, i have no idea what he problem was but your filter seemed to work, however the title of the graph will not appear anymore, any thing need to change in the code?

HI again @raytong I am trying to display a mean calculation underneath the graph of distance error, based on the selection of the drop down, any idea into the best way to solve this?

@rstudionubie. Any reference image about the resulting plot that you wanted?

I cannot find it online, may be too simple i guess. I just want to show the mean of difference error by the inut selected to appear under the graph with a title. I have the code below, but the label says "data" instead of a title.

server <- function(input, output) {
  
  filtered_data<- reactive({
    
    dplyr::filter(spin_rate, spin_rate$Base.Qletter==input$quintile)
    
  })
  
  output$plot <- renderPlot({
    
    #par(mar=c(1,1,1,1))
    
    p<-ggplot(filtered_data()$spin_rate, aes(x=filtered_data()$baseline_spin, y=filtered_data()$Difference.Error))+geom_point(shape=1)
    p+ labs(x="Baseline Technology", y="Difference Error", title = "Difference Error from New Tech by Pitch", colour = "Number Pitch")

  })
  output$meantable<- renderTable({
    mean(filtered_data()$Difference.Error)
  })
  
}

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.