Blog

R and Shiny Web Applications

Shiny and R Talk @ Mango-Solutions

mangologo

First up, we’re doing a talk on R, Shiny and Web Applications @ Chippenham Tech Chat on Weds June 19th 2013. More info here:

hello-shiny

If you’ve not come across it Shiny is a reactive programming web application framework built around R for exposing analytic applications.

It’s made by the guys from RStudio, who also provide the great IDE that lots of us use.

An introduction to Shiny from RStudio:
http://rstudio.github.io/shiny/tutorial/

An introduction to Shiny from the Dallas R users group here:
https://github.com/trestletech/shiny-sandbox/blob/master/shiny_introduction.pdf

and a good overview of deploying Shiny on Amazon EC2 here:
http://www.r-bloggers.com/deploying-shiny-server-on-amazon-ec2/

I guess there’s a few notable things from my side that I thought were pretty great when working with Shiny.

Installation

Get up and running by installing with the following:

 Bash |  copy |? 
install.packages("shiny", repos="http://shiny.rstudio.com")

and then run the example (starts a webserver on port 8100 and invokes xdg-open on Linux)

 Bash |  copy |? 
library(shiny)
runExample("01_hello")

Application Structure

The application consists of two main parts:

User Interface – ui.R
* Layouts
* Panels
* Inputs (selectInput, numericInput etc…)

Serverside – server.R
* function listeners

Architecture

The Shiny architecture is made up of a number of parts internally, namely a set of web/browser components including:

  • Twitter Bootstrap – a UI library that provides scaffolding in terms of layouts with HTML and CSS
  • Websockets – a way to do bi-directional communication with a browser/webserver.

Serverside Shiny makes use of Node.js for hosting (it doesn’t have to), but this to probably the most scalable approach.

Finally, it’s based on the concept of reactive programming which provides a number of parts:

  • Triggers/Inputs – Source (Shiny Reactive Value)
  • Sinks/Outputs – Endpoint (Shiny Observer)

The triggers are executed when dependencies (inputs) change resulting in a change to the output:

 Bash |  copy |? 
shinyServer(function(input, output) {
 output$plotOut <- renderPlot({
   hist(faithful$eruptions, breaks = as.numeric(input$nBreaks))
   if (input$individualObs)
     rug(faithful$eruptions)
 })
 
 output$tableOut <- renderTable({
   if (input$individualObs)
     faithful
   else
     NULL
 })
})

In some cases, changes could take a long time to run (accessing database, read from file). Shiny proposes conductors to encapsulate these operations. Shiny calls these reactive expressions

More info here – http://rstudio.github.io/shiny/tutorial/#reactivity-overview

Deployment

In terms of deployment you have a number of options:

1. You can deploy apps on hosted Shiny Server

2. Run from a Github paste (Gist) or from Github (this is an awesome feature)

 Bash |  copy |? 
shiny::runGist('3239667')
shiny::runGitHub('shiny_example', 'rstudio')

3. Deploy on Amazon EC2

4. Host yourself by installing Apache and NodeJS

Hope that gives you a good overview of Shiny, come along to the meetup if you want to learn more about it.

Recruitment Plug Here ->

Yeah, yeah we’re also recruiting for Java developers, Javascript developers for our devops and development teams here:

http://www.mango-solutions.com/careers.html

plus R developers : )

Our Stackoverflow company page is here – http://careers.stackoverflow.com/company/mango-solutions

Play Framework – Scala TodoList

I’ve just finished going through the Scala TodoList sample application in the Play Framework: http://www.playframework.com/documentation/2.1.0/ScalaTodoList It’s not exactly the most straightforward written tutorial I’ve come across from a framework so I thought I’d post the full sources here along with an archive of everything: Download for people to refer to if they get stuck. One ...

Javascript Visualisation and Graphing

Javascript data visualisation and graphing is becoming a big deal these days. When I started with web development it was on IE5/6 with Javascript and I can’t remember the experience as being pleasurable. Nowadays, our modern browsers, Chrome, Firefox and Opera have HTML5 canvas implementations that provide a mechanism for doing 2D graphics that are ...

Subversion Pre-Commit Hook

Rename the pre-commit.tmpl template to drop the .tmpl off the end. The pre-commit script has to be executable as well by the relevant user.  Bash |  copy |? cd /usr/local/subversioncp pre-commit.tmpl pre-commitchown apache:apache pre-commitchmod +x pre-commit Pre-commit hooks generally call out to other programs to do their work. It’s a Bash script by default, but people tend to write ...

Parsing Kickstarter Information With PHP5, Curl and XPath

I had a simple lightweight task to complete tonight, namely to pull Kickstarter information from the awesome Elite: Dangerous project on Kickstarter. To do it I used a simple PHP5 script with the curl dependency installed with:  Bash |  copy |? sudo apt-get install php5-curl along with a bit of XPath, which can be customised whatever way, script is ...