Dashboards in R with Shiny & Plotly

April 24, 2015

(This article was first published on Modern Data » R, and kindly contributed to R-bloggers)

Shiny is an R application that allows users to build interactive web applications easily in R!

Shiny apps involve two main components: a ui (user interface) script and a server script. The ui script controls the layout of the app and the server script controls what the app does. In other words, the ui script creates what the user sees and controls and the server script completes calculations and creates the plots.

To make a shiny app that is a plotly widget, just add 3 scripts to your app folder in addition to the ui.R and server.R. (1) golbal.R (2) plotlyGraphWidget.R and (3) plotlyGraphWidget.js are all available here! There’s also an optional runApp script that installs the necessary packages and makes it easy to run the app (see instructions below). The plotlyGraphWidget.js script should be inside a folder named www inside the app folder: path

Once all of the components are stored in a folder just open the runApp.R script in rStudio and select “Run App” or, if you have a Shiny Apps account, you can log in then select Publish. After publishing the app, it’s quite easy to embed it in a website like the example apps below!rrr

Alternatively, it’s possible to run the app by setting the working directory to the directory that contains the app folder: setwd(“/Users/…/Shiny”) and then run library(shiny) and runApp(“My_App”).


Grab the scripts here!
This simple example (based on Hello Shiny) uses Plotly syntax to create a histogram of movie ratings (from a ggplot/plotly built in data set) where the user can change the number of bins in the histogram. The scripts are available here. server.R is basically a plotly graph inside a shinyServer function and ui.R consists of a title panel, a sidebar panel with the slider to select the number of bins, and a main panel that displays the graph.

Screen Shot 2015-04-24 at 11.27.40 AMScreen Shot 2015-04-24 at 11.19.58 AM











UN Advanced:
Grab the scripts here or a simpler version here!
This example uses both ggplot and plotly syntax to create the shiny app. The structure of the server script is similar to the one in the example above with an added function: gg<-gg2fig(YOUR_GGPLOT). After this point, you can use plotly syntax to make any additional edits then finally in the return set data = gg$data and layout = gg$layout. This server.R script also includes the code to adjust the title of the graph based on the countries that are selected for the plot and the code to add colored text annotations to the end of each line in the graph.

Grab the scripts here!
This example is adapted from a ggplot/shiny example and uses the built in diamonds dataset. The variables in the graph can be edited to view the data in different ways. In addition to graphing x and y variables, the user can also add an optional color variable, and create multiple plots in columns and/or rows.

To leave a comment for the author, please follow the link and comment on their blog: Modern Data » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)