blog

Embedding Plotly Charts into a Shiny App

Last week I attended a couple of data science talks at the DataPhilly meetup. One of the presenters was Matt Sundquist from Plotly. He gave an overview of some of the capabilities and features of Plotly. During the talk, Matt mentioned that Plotly visualizations can be embedded into a Shiny application. Shiny is a web application framework available in RStudio which allows you to create web applications using only R.

A while back I made a Shiny app as an exercise to learn about the framework using the heart disease data set in the UCI repository. The app uses K-nearest neighbors to classify the presence or absence of heart disease given several features. I decided to alter the app using Plotly graphs in place of the ggplot2 graphs in the original app. You can try both apps out and get the code at the links below. The graphs can be found by clicking the Visualize Features tab in the app. (NOTE: The apps are embedded below for convenience and may appear somewhat compressed. For the best visability you may want to view the app in a separate window by clicking the provided links)

Overview of the Shiny App Layout

The apps contains 4 tabs:

Classifier - This tab contains slider bar for selection of k and check boxes for features to be included during the classification. A confusion matrix and the classification error value for the selected parameters is displayed.

Visualize Features - This tab allows the user to view the distribution of two features and the two features plotted against one another. The color in the plots is based on the presence (positive) or absense (negative) of heart disease in the dataset.

Feature Description - Provides a short description of each feature

References - Data source references.

Embedding Plotly in the App

The plots present in the Visualize Features tab were originally created using the ggplot2 package. To implement Plotly graphs in place of the old graphs a few new functions were necessary. Lucky for me Plotly provides some examples in GitHub which I used as my starting point.

To provide the new functionality to my app I used the two files listed below:

plotlyGraphWidget.R

plotlyGraphWidget.js

Both files can be found in the ggplot2 UN example. I copied both the files into my Shiny app project folder. (NOTE: For the plotlyGraphWidget.js file I copied the complete www directory.)

The plotlyGraphWidget.R script contains two functions: graphOutput, which will be used to display the plot in the ui.R file, and renderGraph, which is used in the server.R file. The original app uses the plotOutput function in the ui.R file to define where a plot is shown.

After updating the ui.R file, the server.R file needs to be updated. The first step is to replace the instances of renderPlot with renderGraph. In the base app a ggplot object was created inside the renderPlot function, but to use Plotly the ggplot object must be converted to a list containing the plot details. This task is handled by the gg2list function provided in the plotly library.