Skip to content

Instantly share code, notes, and snippets.

@karawoo
Created July 31, 2015 21:34
Show Gist options
  • Save karawoo/fa787eb0ecb0e3777fd7 to your computer and use it in GitHub Desktop.
Save karawoo/fa787eb0ecb0e3777fd7 to your computer and use it in GitHub Desktop.
Toggling user-selected layers with ggplot2 in a Shiny app
library("shiny")
library("ggplot2")
shinyServer(function(input, output) {
output$myplot <- renderPlot({
## Create a boxplot with optional jittering
p <- ggplot(iris, aes(x = Species, y = Petal.Length)) +
geom_boxplot() +
ylab("Petal Length")
if (input$optlayer) {
p <- p + geom_jitter()
}
p
})
})
library("shiny")
shinyUI(pageWithSidebar(
## App title
titlePanel("Toggling User-Selected Layers"),
# Sidebar with a layer that user can choose to add
sidebarPanel(
checkboxInput("optlayer", label = "Jitter", value = FALSE)
),
## Show plot in main panel
mainPanel(
plotOutput("myplot")
)
)
)
@karawoo
Copy link
Author

karawoo commented Jul 31, 2015

output

@ds-jim
Copy link

ds-jim commented Sep 9, 2021

Lovely, nice solution to the problem :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment