Add ggplot geoms incrementally from UI in shiny

Hello,

I'm interested in adding ggplot2 geoms incrementally in a Shiny app plot from user input. I was hoping to utilize the checkboxGroupInput function, but am having issues when no choices are selected or more than one choice is selected. Also, the conditionals on the server side are probably too verbose, so there's probably a better solution.

A reprex is below. Any suggestions are appreciated!

# CheckboxGroupInput Reprex
library(shiny)
library(ggplot2)

# Define UI
ui <- fluidPage(

    # Application title
    titlePanel("CheckboxGroupInput Reprex"),

    # Sidebar with a checkboxGroupInput
    sidebarLayout(
        sidebarPanel(
            checkboxGroupInput("vars", label = "Select geoms to add:", 
                               choices = c("Points", 
                                           "Lines", 
                                           "Smooth"))
        ),

        # Show a plot of variable geoms
        mainPanel(
           plotOutput("irisPlot")
        )
    )
)

# Define server logic
server <- function(input, output) {

    output$irisPlot <- renderPlot({
        
      
      p1 <- iris %>%
        ggplot(aes(x = Sepal.Length, y = Sepal.Width, group = Species))
      
      if(input$vars == "Points"){
        
        p1 + geom_point()
      } else if(input$vars == "Lines"){
        
        p1 + geom_line()
      } else if(input$vars == "Smooth"){
        
        p1 + geom_smooth()
      } else if(input$vars == "Points" & input$vars == "Smooth"){
        
        p1 + geom_point() + geom_line()
      } else{
        
        p1
      }
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

This still has some rough edges, but does it do generally what you want?

# CheckboxGroupInput Reprex
library(shiny)
library(ggplot2)

# Define UI
ui <- fluidPage(
  
  # Application title
  titlePanel("CheckboxGroupInput Reprex"),
  
  # Sidebar with a checkboxGroupInput
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("vars", label = "Select geoms to add:", 
                         choices = c("Points", 
                                     "Lines", 
                                     "Smooth"))
    ),
    
    # Show a plot of variable geoms
    mainPanel(
      plotOutput("irisPlot")
    )
  )
)

# Define server logic
server <- function(input, output) {
  
  output$irisPlot <- renderPlot({
    
    
    p1 <- iris |>
      ggplot(aes(x = Sepal.Length, y = Sepal.Width, group = Species))
    
    if("Points" %in% input$vars){
      
     p1 <-  p1 + geom_point()
    } 
    if("Lines" %in% input$vars ){
      
    p1 <-   p1 + geom_line()
    } 
    if("Smooth" %in% input$vars){
      
    p1 <-   p1 + geom_smooth()
    }
    p1
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

That does it. I should've known to try the %in% operator.

Thanks, FJCC!

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.