Created
November 15, 2012 00:44
-
-
Save cengel/4075892 to your computer and use it in GitHub Desktop.
Shiny intro tutorial example rewritten for ggplot2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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