Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
103 lines (81 sloc) 4.1 KB

opts_chunk$set(warning=FALSE, message=FALSE, comment=NA, cache=FALSE)

name: shiny-r layout: post title: Shiny apps are awesome date: 2012-12-10 author: Scott Chamberlain tags:

  • R
  • RStudio
  • ropensci
  • shiny

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).


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.


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

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

Here's the server logic

library(shiny); library(plyr); library(ggplot2); library(rgbif)

## Set up server output
shinyServer(function(input, output) {
  # define function for server plot output
  output$gbifplot <- reactivePlot(function() {
    species <- input$spec
    df <- out[names(out) %in% species]
  output$cbt <- reactiveText(function(){

The user interface


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

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 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.