When you want to get to know and love your data

Dynamic Data Visualizations in the Browser Using Shiny


After being busy the last two weeks teaching and attending academic conferences, I finally found some time to do what I love, program data visualizations using R. After being interested in Shiny for a while, I finally decided to pull the trigger and build my first Shiny app!

I wanted to make a proof of concept app which contained the following dynamics which are the basics of any UI design:

1) dynamic UI options

2) dynamically updated plot based on UI inputs

Here is what I came up with.


Check out the app for yourself  or the R code HERE.


The app consists of a user interface (UI)  for selecting the data, variable to plot , grouping factor for colors and four plotting options: boxplot (above), histogram, density plot and bar graph. As an added bonus the user can select to show or hide jittered points in the boxplot visualization.

Generally #2 above was well described and easy to implement, but it took a lot of trial and error to figure out how to implement #1. Basically to generate dynamic UI objects, the UI objects need to be called using the function shiny:::uiOutput()  in the ui.R file and their arguments set in the server.R file using the function shiny:::renderUI(). After getting this to work everything else fell in place.

Having some experience with making UI’s in VBA (visual basic) and gWidgets; Shiny is a joy to work with once you understand some of its inner workings. One aspect I felt which made the learning experience frustrating was the lack of informative errors coming from Shiny functions. Even using all the R debugging tools having Shiny constantly tell me something was not correctly called from a reactive environment or the error was in the runApp() did not really help. My advice to anyone learning Shiny is to take a look at the tutorials, and particularly the section on Dynamic UI. Then pick a small example to reverse engineer. Don’t start off too complicated else you will have a hard time understanding which sections of code are not working as expected.

Finally here are some screen shots, and keep an eye out for more advanced shiny apps in the near future.

density plot histogram bar


7 responses

  1. Cool post!

    Just FYI, dynamic UI is no longer needed to accomplish the dynamic population of select inputs (and other controls). Try this:

    July 4, 2013 at 7:44 pm

  2. Sungjoon Nam

    How are you? I am trying to run your shiny code for PCA and ggplot2.
    However, an error comes up, and I don’t know how to fix it.
    Errors are following
    Downloading https://gist.github.com/5846650/download

    Listening on port 8100
    Error in .Call(“R_fromJSON”, content, as.integer(sum(simplify)), nullValue, :
    “R_fromJSON” not resolved from current namespace (RJSONIO)
    In addition: Warning messages:
    1: In download.file(url, …) :
    downloaded length 2820 != reported length 200
    2: In mySI2(internet2_start) : internet routines were already initialized

    October 20, 2013 at 7:19 am

    • Hi,

      Based on your RJSONIO error:
      “Error in .Call(“R_fromJSON”, content, as.integer(sum(simplify)), nullValue, :”R_fromJSON” not resolved from current namespace (RJSONIO)”
      Try to close R, reopen and then run the code again. Things should work the second time. I’ll try to push the fix for this bug soon.


      October 21, 2013 at 5:33 pm

  3. Bee

    Hi, I’m trying to use your fantastic example with an external uploaded file but I get the error “invalid first argument”, could you help me? Thank you
    (I wrote here my code https://community.rstudio.com/t/upload-a-file-and-make-reactive-boxplots/4773)

    February 1, 2018 at 4:45 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s