Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
184 lines (138 sloc) 5.22 KB
---
title: "shiny_workshop"
author: "Sebastien Renaut"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
html_document:
number_sections: T
toc: yes
toc_depth: 3
toc_float:
collapsed: T
runtime: shiny
---
```{r setup, echo=T}
#install.packages("shiny")
library(shiny)
```
# What is Shiny?
* This `R Markdown'`html document is made interactive using `shiny`.
* To reate documents that allow your readers to change the assumptions underlying your analysis and see the results immediately.
* To learn more, see [Interactive Documents](http://rmarkdown.rstudio.com/authoring_shiny.html).
# Set-up
## Header
>\---
>title: "shiny_workshop"
>author: "Sebastien Renaut"
>date: "`r format(Sys.time(), '%B %d, %Y')`"
>output: html_document
>runtime: shiny
>\---
## View document with shiny
* Run document (instead of Knit)
## Inputs and Outputs
* Outputs are automatically updated whenever inputs change.
* The code below demonstrates how a standard plot can be made interactive by wrapping it in the Shiny `renderPlot` function (*output*). The `selectInput` and `sliderInput` functions create the *input* widgets used to generate plots.
```{r eruptions, echo=T}
#Here we will use the faithful geyser example dataset to generate histogram and a density function.
#Go see what the data looks like
head(faithful)
#What input will you give to your function:
inputPanel(
#selecting the type of input (n_breaks)
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
#selecting the type of input (bw_adjust)
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
#What plot will you make:
renderPlot({
#an histogram of faithful$eruptions with breaks defined according to above
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
#a density line of faithful$eruptions with adjust defined according to above
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
## Embedded Application
* It's also possible to embed an entire Shiny application within an R Markdown document using the `shinyAppDir` function.
```{r tabsets, echo=T}
shinyAppDir(
system.file("examples/06_tabsets", package = "shiny"),
options = list(
width = "100%", height = 550
)
)
```
* Note the use of the `height` parameter to determine how much vertical space the embedded application should occupy.
## Another (simple) example
* See [gallery](https://shiny.rstudio.com/gallery/) for more examples.
```{r mtcars, echo=T}
# Define UI for miles per gallon app ----
ui <- fluidPage(
# App title ----
titlePanel("Miles Per Gallon"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Selector for variable to plot against mpg ----
selectInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"))
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Formatted text for caption ----
h3(textOutput("caption")),
# Output: Plot of the requested variable against mpg ----
plotOutput("mpgPlot")
)
)
)
# Define server logic to plot various variables against mpg ----
server <- function(input, output) {
# Compute the formula text ----
# This is in a reactive expression since it is shared by the
# output$caption and output$mpgPlot functions
formulaText <- reactive({
paste("mpg ~", input$variable)
})
# Return the formula text for printing as a caption ----
output$caption <- renderText({
formulaText()
})
# Generate a plot of the requested variable against mpg ----
# and only exclude outliers if requested
output$mpgPlot <- renderPlot({
plot(as.formula(formulaText()),
data = mtcars,
col = "#75AADB", pch = 19)
})
}
# Return a Shiny app object
shinyApp(ui = ui, server = server,options=list(height = 550))
```
# Deployment
* When you run a `shiny` document locally, it uses your local R session. Only you can see and interact with the document.
* The power of `shiny` comes from a live R session running behind the scenes to all allow users to interct with documents.
* You will have to deploy the document on a server, and share the URL of the document. Then other people only need a web browser to visit your document.
* Two ways to deploy a `shiny` document:
+ Host it by service provided by **RStudio**
+ Technically easier.
+ webpage hosted on shinyapps.io
+ Freemium businness model
+ Set up your own server
+ Independence from *Rstudio*
# ShinyApps.io
* Sign up to http://www.shinyapps.io/
* Connect to the remote server (may need to install `rsconnect` and `packrat`)
*Also need to set remote account info
+ Once signed up, go to http://www.shinyapps.io/ >Account >Tokens >Show
>
>rsconnect::setAccountInfo(name='yourname',token='alphanumeric',secret='ASCII')
>
* Publish document in Rtudio by clicking on *blue eye* at the top right corner. Voilà!