jeudi 24 août 2023

Add or remove multiple horizontal lines in Shinly ggplot2

I have a shiny app that plots the data from an upload file. I have couple of checkboxInput & couple of corresponding numericInput in the app. Plotting works fine with numericInputs, as in below simple reproducible example. What I want to achieve here is, be able to select or deselect that specific checkbox so that geom_hline is added or removed from the plot.

Users can select or deselect one or multiple at a time based on the requirement.

Thanks in advance

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "H Line Plot Add & Remove"),
  dashboardSidebar(checkboxInput("minthresh", "Mimimun Threshold", T),
                   numericInput("minthreshvalue", "Minimum Threshold Value:", min = 0, max = 9, value = 5),

                   checkboxInput("maxthresh", "Maximun Threshold", T),
                   numericInput("maxthreshvalue", "Maximum Threshold Value:", min = 0, max = 9, value = 6),

                   checkboxInput("optthresh", "Optimal Threshold", T),
                   numericInput("optthreshvalue", "Optimal Threshold Value:", min = 0, max = 9, value = 7)),
  dashboardBody(plotOutput("plot1"))
)

server <- function(input, output) {
  data <- iris

  output$plot1 <- renderPlot({
    ggplot(data, aes(x = Species, y = Sepal.Length)) +
      theme(axis.text.x = element_text(size = 20),
            axis.text.y = element_text(size = 20)) +
      geom_boxplot() +
      geom_hline(aes(yintercept = input$minthreshvalue), color = "red", size = 3) +
      geom_hline(aes(yintercept = input$maxthreshvalue), color = "blue", size = 3) +
      geom_hline(aes(yintercept = input$optthreshvalue), color = "darkgreen", size = 3)
  })
}

shinyApp(ui, server)



Aucun commentaire:

Enregistrer un commentaire