Shiny apps are awesome

[This article was first published on Recology - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

RStudio has a new product called Shiny that, quoting from their website, “makes it super simple for R users like you to turn analyses into interactive web applications that anyone can use”. See here for more information.

A Shiny basically consists of two files: a ui.r file and a server.r file. The ui.r file, as it says, provides the user interface, and the server.r file provides the the server logic.

Below is what it looks like in the wild (on a browser).

center

It was pretty easy (for Ted Hart of rOpenSci) to build this app to demonstrate output from the ropensci rgbif package.


You may need to install packages first.

install.packages<span class="p">(</span>c<span class="p">(</span><span class="s">"shiny"</span><span class="p">,</span> <span class="s">"ggplot2"</span><span class="p">,</span> <span class="s">"plyr"</span><span class="p">,</span> <span class="s">"rgbif"</span><span class="p">))</span>

We tried to build in making real time API calls to GBIF’s servers, but the calls took too long for web speed. So we prepare the data first, and then serve it up from saved data in a .rda file. Let’s first prepare the data. –Well, this is what we do on the app itself, but see the next code block for

library<span class="p">(</span>rgbif<span class="p">)</span>
splist <span class="o"><-</span> c<span class="p">(</span><span class="s">"Accipiter erythronemius"</span><span class="p">,</span> <span class="s">"Junco hyemalis"</span><span class="p">,</span> <span class="s">"Aix sponsa"</span><span class="p">,</span> <span class="s">"Haliaeetus leucocephalus"</span><span class="p">,</span> 
    <span class="s">"Corvus corone"</span><span class="p">,</span> <span class="s">"Threskiornis molucca"</span><span class="p">,</span> <span class="s">"Merops malimbicus"</span><span class="p">)</span>
out <span class="o"><-</span> llply<span class="p">(</span>splist<span class="p">,</span> <span class="kr">function</span><span class="p">(</span>x<span class="p">)</span> occurrencelist<span class="p">(</span>x<span class="p">,</span> coordinatestatus <span class="o">=</span> <span class="k-Variable">T</span><span class="p">,</span> maxresults <span class="o">=</span> <span class="m">100</span><span class="p">))</span>
names<span class="p">(</span>out<span class="p">)</span> <span class="o"><-</span> splist  <span class="c1"># name each data.frame with the species names</span>
setwd<span class="p">(</span><span class="s">"~/ShinyApps/rgbif2"</span><span class="p">)</span>  <span class="c1"># set directory</span>
save<span class="p">(</span>out<span class="p">,</span> file <span class="o">=</span> <span class="s">"speciesdata.rda"</span><span class="p">)</span>  <span class="c1"># save the list of data.frames into an .rda file to serve up</span>

Here’s the server logic

library<span class="p">(</span>shiny<span class="p">)</span>
library<span class="p">(</span>plyr<span class="p">)</span>
library<span class="p">(</span>ggplot2<span class="p">)</span>
library<span class="p">(</span>rgbif<span class="p">)</span>

<span class="c1">## Set up server output</span>
shinyServer<span class="p">(</span><span class="kr">function</span><span class="p">(</span>input<span class="p">,</span> output<span class="p">)</span> <span class="p">{</span>
    load<span class="p">(</span><span class="s">"speciesdata.rda"</span><span class="p">)</span>
    <span class="c1"># define function for server plot output</span>
    output<span class="o">$</span>gbifplot <span class="o"><-</span> reactivePlot<span class="p">(</span><span class="kr">function</span><span class="p">()</span> <span class="p">{</span>
        species <span class="o"><-</span> input<span class="o">$</span>spec
        df <span class="o"><-</span> out<span class="p">[</span>names<span class="p">(</span>out<span class="p">)</span> <span class="o">%in%</span> species<span class="p">]</span>
        print<span class="p">(</span>gbifmap<span class="p">(</span>df<span class="p">))</span>
    <span class="p">})</span>
    output<span class="o">$</span>cbt <span class="o"><-</span> reactiveText<span class="p">(</span><span class="kr">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="p">})</span>
<span class="p">})</span>

The user interface

library<span class="p">(</span>shiny<span class="p">)</span>

<span class="c1"># Define UI for application that plots random distributions</span>
shinyUI<span class="p">(</span>pageWithSidebar<span class="p">(</span>headerPanel<span class="p">(</span><span class="s">"rgbif example"</span><span class="p">),</span> sidebarPanel<span class="p">(</span>checkboxGroupInput<span class="p">(</span><span class="s">"spec"</span><span class="p">,</span> 
    <span class="s">"Species to map:"</span><span class="p">,</span> c<span class="p">(</span><span class="sb">`Sharp shinned hawk (Accipiter erythronemius)`</span> <span class="o">=</span> <span class="s">"Accipiter erythronemius"</span><span class="p">,</span> 
        <span class="sb">`Dark eyed junco (Junco hyemalis)`</span> <span class="o">=</span> <span class="s">"Junco hyemalis"</span><span class="p">,</span> <span class="sb">`Wood duck (Aix sponsa)`</span> <span class="o">=</span> <span class="s">"Aix sponsa"</span><span class="p">,</span> 
        <span class="sb">`Bald eagle (Haliaeetus leucocephalus)`</span> <span class="o">=</span> <span class="s">"Haliaeetus leucocephalus"</span><span class="p">,</span> 
        <span class="sb">`Carrion crow (Corvus corone)`</span> <span class="o">=</span> <span class="s">"Corvus corone"</span><span class="p">,</span> <span class="sb">`Australian White Ibis (Threskiornis molucca)`</span> <span class="o">=</span> <span class="s">"Threskiornis molucca"</span><span class="p">,</span> 
        <span class="sb">`Rosy Bee-eater (Merops malimbicus)`</span> <span class="o">=</span> <span class="s">"Merops malimbicus"</span><span class="p">),</span> selected <span class="o">=</span> c<span class="p">(</span><span class="s">"Bald eagle (Haliaeetus leucocephalus)"</span><span class="p">))),</span> 
    mainPanel<span class="p">(</span>h5<span class="p">(</span><span class="s">"A map of your selected species: Please note that GBIF is queried for every selection so loading times vary"</span><span class="p">),</span> 
        plotOutput<span class="p">(</span><span class="s">"gbifplot"</span><span class="p">))))</span>

This should be all you need. To actually serve up the app in the web, request to be part of their beta-test of Shiny server on the web here.

Go play with our Shiny app here to see the kind of visualization you can do with the rgbif package.


Get the .Rmd file used to create this post at my github account – or .md file.

Written in Markdown, with help from knitr.

To leave a comment for the author, please follow the link and comment on their blog: Recology - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)