Shiny for Interactive Apps

Albert Y. Kim
Friday 2016/3/25

Intro to Shiny

Today we will create interactive web applications via Shiny. Shiny allows you to do so without knowing HTML, JavaScript, CSS, etc…

For the rest of this talk, if you see USERNAME, replace this with your Middlebury login.

Shiny Approaches

There are many ways to create a Shiny App

  1. R Markdown: New File -> R Markdown… -> Shiny -> either a document or slides
  2. Single File App: New File -> Shiny Web App -> Single File app.R
  3. Multiple File App (allows a more split organization): New File -> Shiny Web App -> Multiple File ui.R/server.R

…but they all share certain components. We focus on the 2nd.

Shared Components

  1. UI user interface component: what the user sees
    • Panel: organize the layout
    • SOMETHINGInput: App takes inputs and saves them in input. Ex: text, numerical values, sliders, radio buttons, etc.
    • renderSOMETHING: Shiny renders an output. Ex: plots, table, text, etc.
  2. server: the R code that does the work behind the scenes

Then run it!

New Shiny App

Let's create a new single file Shiny app app.R:

  • New File -> Shiny Web App…
  • Call it testing and “single file” application type.
  • Click on “Run App”

UI Component: Set Panels

  • Use to say layout and organization of inputs and outputs section
  • Section 5 of cheat sheet, on back

UI Component: Get Inputs

  • sliderInput() assign a value to bins based on a slider
  • All possible input options are listed in “Widgets” on the cheatsheet.
  • Type ?selectInput, for example, to get a sense for the arguments.

UI Component: Render Outputs

  • plotOutput("distPlot") renders the output defined in the server section. The server section does the
  • All possible rendering options are listed in “render functions” on the cheatsheet

Server Component: R Code

  • Whatever you are going to output needs to be assigned to an object output
  • Described on front of cheat sheet

Publishing Apps

To publish your apps:

  • Go to https://www.shinyapps.io/ and create a free account. You can host up to 5 shiny apps. However, you only get limited server resources dedicated to each user.
  • Even better: Use the recently installed Middlebury Shiny Server Pro.

Uploading Files to Midd Shiny Server

Mac:

  • Open Finder
  • Menu Bar -> Go > Connect to Server: cifs://shiny.middlebury.edu/USERNAME

Windows:

  • Open File Explorer
  • In address bar, \\shiny.middlebury.edu\USERNAME

Uploading Files to Midd Shiny Server

Once logged in:

  • Go into the folder ShinyApps
  • Create a folder with the name of your Shiny App

The URL any Shiny App APPNAME is

Installing Packages

For now, this is only way I know; I'm investigating if this can be easier.

  • RStudio menu bar -> Tools -> Shell…
  • Type ssh USERNAME@shiny.middlebury.edu
  • Type R.
  • Type install.packages("ggplot2") for example
  • Select a CRAN mirror in the United States.
  • Say yes to prompts about installing in a new library.
  • After you've installed all the necessary packages, close the window.

Today's Exercise: Publish Shiny App

First

Today's Exercise: Publish Shiny App

Second

  • Change the histogram in the app.R file to be a ggplot, but keep the slider so that it sets the bins argument in geom_histogram().
  • Test it locally on your computer.
  • Once it works, install ggplot2 and dplyr on Shiny Server.
  • Update app.R to be the new app.
  • Test that you can access it from a browser.

Other Resources