Skip to content

Building my first Shiny application with ggplot

In trying to get a grip on the newly released Shiny library for R I simply rewrote the example from the tutorial to work with ggplot. The code is taken from the Shiny Tutorial.

I did not make any changes to ui.R provided in the tutorial. The rewritten server.R is below.

No TweetBacks yet. (Be the first to Tweet this post)

Categories: Noteworthy Bits.

Tags: , , , ,

Comment Feed

5 Responses

  1. Nice job, I had to do something similar recently.

  2. Thank you for this.

  3. was wondering why it my ggplot histogram did not I see i need the print line. Thanks!

  4. Thanks for sharing! small changes were made to the syntax apparently, this variant worked:


    # We tweak the "am" field to have nicer factor labels. Since this doesn't
    # rely on any user inputs we can do this once at startup and then use the
    # value throughout the lifetime of the application
    mpgData <- mtcars
    mpgData$am <- factor(mpgData$am, labels = c("Automatic", "Manual"))

    # Define server logic required to plot various variables against mpg
    shinyServer(function(input, output) {

    # Compute the forumla text in a reactive expression since it is
    # shared by the output$caption and output$mpgPlot expressions
    formulaText <- reactive({
    paste("mpg ~", input$variable)

    # Return the formula text for printing as a caption
    output$caption <- renderText({

    # Generate a plot of the requested variable against mpg and only
    # include outliers if requested
    # ggplot version
    output$mpgPlot <- renderPlot({

    # 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)) +


  5. Nice job and thanks. I have recently discovered shiny and gonna try to put my ggplot scripts on shiny. Thanks.

    Paco PastorNovember 18, 2013 @ 9:37 amReply

Some HTML is OK

or, reply to this post via trackback.