ggplot filter on shiny app

Hi everyone,

I am a beginner in R and I am trying to create a simple Shinyapp and I have a problem of filtering my data to have a reactive plot depending on the filter. This is for a sport application (i.e. ice hockey)

I want to filter my data (2 differents metrics) depending on position or not. If I select anything in my sidebarpanel I want the mean of all my position if i select "position" i would like graphs of my two metrics split by position.

If anyone has a tip for that.

ui <- fluidPage(
  titlePanel(
    "CHARGE EXERCICE"
  ),
  sidebarPanel(
    width = 3,
    selectizeInput(inputId = "Exercice",
                   label = "Drill",
                   choices = unique(df$Exercice),
                   selected = "",
                   multiple = TRUE),
    
  ),
  
  mainPanel(
    plotOutput(outputId = "TRIMP.min"),
    plotOutput(outputId = "Mvt_intensity")
  )
)


## server

server <- function(input, output, session){
  
  dat <- reactive({
    
    d <- df %>%
      filter(Exercice == input$Exercice)
      
    d
  })
  
  output$TRIMP.min <- renderPlot({
    
    dat() %>% 
     ggplot(
        aes(
          x = Exercice,
          y = TRIMP.min,
          )) +
      labs(y = "Intensite Cardio", x="") +
      theme(axis.title.y = element_text(size = 14, face = "bold"))+
      theme(axis.text.x = element_text(size = 14, face = "bold"))+
      geom_bar(stat = "summary", fun = mean, fill = "red", )+
      geom_hline(yintercept = mean(df$TRIMP.min), linetype = "dashed", size = 1, color="black") 
  })
  
  output$Mvt_intensity <- renderPlot({
    
    dat() %>% 
      ggplot(
        aes(
          x = Exercice,
          y = Mvt_intensity,
        )) +
      labs(y = "Intensite Mouvement", x="") +
      theme(axis.title.y = element_text(size = 14, face = "bold"))+
      theme(axis.text.x = element_text(size = 14, face = "bold"))+
      geom_bar(stat = "summary", fun = mean, fill = "black") +
      geom_hline(yintercept = mean(df$Mvt_intensity), linetype = "dashed", size = 1, color="white")
    
  })
  
}



shinyApp(ui, server)

I have two different position in my data (Defense and Forward)

Hi, there are lots of examples of filtering in shiny apps on the forum and elsewhere. e.g.:

It will help if your example is reproducible too.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.