Do you want to create R functions that can be accessed by non-R users? Do you want your R code to be interactive and alive? Shiny is a freely-available R package that provides a programming environment to turn your R analyses into interactive web applications. In this presentation, I will introduce the Shiny package and its basic structure. It enables our R code to dynamically react to user input, and it is straight-forward to implement without any prior knowledge in website design. I will also demonstrate some examples of web applications designed using Shiny.
December 2015 Meetup - Shiny: Make Your R Code Interactive - Craig Wang
1. Shiny: Make Your R Code
Interactive
Craig Wang
MScin StatisticsatETH
R Programmerat Institutfür MathematikUZH
Zurich R User Group Meetups
December
1
3. Overview
• Introduction
• Setup and structure
• Work-through example
• Deploy and Share
• More Resources
3
Zurich R User Group Meetups
December
4. Introduction to Shiny
• A freely-available third party R package created by Rstudio
• Allows to transform R code into interactive web applications
• Provide native programming environments
• Easy to get hands on, no website design experience required
4
Zurich R User Group Meetups
December
5. Setup and Structure
• Simply install and load the package ‘shiny’
• Select a path to save all your files about your Shiny application
• Basic files
• ui.R is a script file that implements all the user interface elements such as
buttons,drop-downmenus, sliders
• server.R is a scriptfile that takes input from the interfaceand processit,
finally update the interface
• runApp(“Hello Shiny”) in R
5
Zurich R User Group Meetups
December
7. Work-through Example
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = n + 1) # n is number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
7
Zurich R User Group Meetups
December
8. Work-through Example
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = n + 1) # n is number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
8
Zurich R User Group Meetups
December
9. Work-through Example
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = n + 1) # n is number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
library(shiny)
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
9
Zurich R User Group Meetups
December
11. Deploy and Share
• Once the application is ready, it is free to share with the public
• Cost: free for 25 hours/month, up to 10,000 hours/month
• Wide range of applications on Shinyapp.io
• https://jnuke2000.shinyapps.io/ShinyDash
• https://leehbi.shinyapps.io/Sales-Pricing-Tool/
• https://healthrank.shinyapps.io/demo/
11
Zurich R User Group Meetups
December
12. More Resources
• Cheat sheets
• Tutorial
• Shiny gallery
12
Zurich R User Group Meetups
December
13. Final Notes
• Demo website: http://t.uzh.ch/se
• My contact: craigwang247@gmail.com
• Thanks Christoph and Heidi for organizing this, also Sanitas for
sponsoring the venue.
13
Zurich R User Group Meetups
December