public
Created

Shiny intro tutorial example rewritten for ggplot2

  • Download Gist
server.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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)
})
})

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.