Skip to content

Instantly share code, notes, and snippets.

@cengel
Created November 15, 2012 00:44
Show Gist options
  • Save cengel/4075892 to your computer and use it in GitHub Desktop.
Save cengel/4075892 to your computer and use it in GitHub Desktop.
Shiny intro tutorial example rewritten for ggplot2
library(shiny)
library(datasets)
library(ggplot2) # load ggplot
# Define server logic required to plot various variables against mpg
shinyServer(function(input, output) {
# Compute the forumla text in a reactive function since it is
# shared by the output$caption and output$mpgPlot functions
formulaText <- reactive(function() {
paste("mpg ~", input$variable)
})
# Return the formula text for printing as a caption
output$caption <- reactiveText(function() {
formulaText()
})
# Generate a plot of the requested variable against mpg and only
# include outliers if requested
# ggplot version
output$mpgPlot <- reactivePlot(function() {
# check for the input variable
if (input$variable == "am") {
# am
mpgData <- data.frame(mpg = mtcars$mpg, var = factor(mtcars[[input$variable]], labels = c("Automatic", "Manual")))
}
else {
# cyl and gear
mpgData <- data.frame(mpg = mtcars$mpg, var = factor(mtcars[[input$variable]]))
}
p <- ggplot(mpgData, aes(var, mpg)) +
geom_boxplot(outlier.size = ifelse(input$outliers, 2, NA)) +
xlab(input$variable)
print(p)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment