Deploying a graph as a service via Shiny

plotly

#1

Hi all,

On our team when we want to produce a website that includes an interactive data visualization we often follow a pattern where our web developer constructs most of the site and I produce a Shiny app that we include within an iframe. Now we are facing a project where there will be a number of these visualizations, which for each I will need to write a separate app that will go into a separate iframe, and I am starting to think there must be a better way.

Is it possible to use a Shiny Server installation to create an app that behaves like a web service? I'm thinking it could receive the inputs through JSON or maybe even directly read HTML input controls and produce an output like a plotly chart in JavaScript that our web developer could insert into a page without having to use an iframe.

I see there are alternatives out there like OpenCPU, but I'm hoping to not have to go to our IT asking them to support yet another infrastructure if Shiny Server can do the job.


#2

You bet! I should just mention though that if your team migrates from shiny server to connect (which is likely easier than you might think), you could leverage "lighter-weight" solutions, like plumber, for this sort of thing.

I'm thinking it could receive the inputs through JSON or maybe even directly read HTML input controls and produce an output like a plotly chart

I'm not sure how you'd like to create the JSON, but keep in mind that you can get the JSON behind any R-based plotly graph via plotly_json(). Also, be aware that the R package has an additional JSON-spec and JavaScript layer to enable things that you won't find in other languages (e.g., python) and won't be easy to support without going the iframe route. However, as long as you're charts aren't leveraging anything specific the R library, in theory, you could have your shiny 'web service' generate JSON using plotly_json() and send it to the larger application that uses plotly.js directly to render the chart.


#3

Connect and plumber certainly look like interesting options, but you say it is possible using our existing Shiny infrastructure? I haven't been able to find any documentation on this.

Basically I want to leave the web design to the web developer and the data processing to the R programmer and make the connection in between as smooth as possible, while keeping the maintenance tasks for our IT admins minimal. Personally I only really understand the R part, but I'm trying to put together enough information to converse intelligently with the people who represent the web and admin parts.

I'll have to think about whether these charts are specifically leveraging R - I'm not clear on where to draw the line because I'm coding everything in R. Is it just a matter of having all data filtering and aggregating done before passing it to the plotly function? And then the plot parameters and data get passed as JSON via e.g. plumber for the web dev to consume?


#4

Connect and plumber certainly look like interesting options, but you say it is possible using our existing Shiny infrastructure? I haven't been able to find any documentation on this.

To say for sure, I'd need to know more about your project, but you can use shiny to generate and serve assets that other programs can request (i.e., act like a web service). The plumber package is designed and optimized for that sort of task. I highly recommend watching this talk.

You might also consider just varying the logic in your shiny app based on the URL parameters given to the application.

I think a critical question to consider is, who is managing the input values (i.e., the input parameters that generate your graph), you or the web developer? If you need to manage them, then you likely want to use the resources I point to above. If your web developer is managing them, you're probably better off hosting (plumber-based) HTTP endpoints that the developer can request.


#5

For the record, we had a feature like this slated for Shiny but it got kind of stalled along the way for a variety of reasons:
https://www.rstudio.com/resources/videos/whats-new-with-shiny/
(fast forward to 15:04)

It could come back in a future release but is not currently on the schedule.