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)