Jonathan Hill
2016-02-26
Everything's on GitHub!
install.packages("shiny")
require(shiny)
runExample()
Valid examples are "01_hello", "02_text", "03_reactivity", "04_mpg", "05_sliders", "06_tabsets", "07_widgets", "08_html", "09_upload", "10_download", "11_timer"
runExample("01_hello")
Runs once during initialization
# -- global.R -- #
library(dplyr)
df <- read.csv("file.csv")
Runs throughout the session
# -- server.R -- #
server <-
function(input, output, session) {
# regular computations
}
server <-
function(input, output, session) {
output$<id> <- renderPlot({
# user input will trigger this statement #
x <- mtcars[ , input$<id> ]
plot(x, y)
})
}
Must be placed within one of the following functions:
| Function | Output |
|---|---|
| render | a shiny UI component |
| reactive | a reactive expression |
| observe | a reactive observer |
| isolate | a non-reactive copy of a reactive object |
When an input changes the server will rebuild each output that depends on it (event if that dependence is indirect). You can control that process with the following functions:
output$z <- renderText({
input$a
})
An output automatically updates whenever an input changes
Used to create objects that will be used in multiple outputs:
x <- reactive({ input$a })
output$y <- renderText({ x() })
output$z <- renderText({ x() })
Shiny will only update z when b changes:
output$z <- renderText({
paste(
isolate(input$a),
input$b
)
})
Use observers to contain computationally intensive pieces of your server:
observeEvent(input$submit, {
cashflow <- function(
loanTerm = input$loan_term,
defaultRate = input$default_rate,
...
)
})
# -- ui.R -- #
ui <- fluidPage(
textInput("a", "")
)
ui
## <div class="container-fluid">
## <div class="form-group shiny-input-container">
## <label for="a"></label>
## <input id="a" type="text"
## class="form-control" value=""/>
## </div>
## </div>
HTML('complete layout here')
ui <- fluidPage(
fluidRow(column(width = 4),
column(width = 4),
column(width = 4)
),
fluidRow(column(width = 12)
)
)
h1("Header 1")
br()
p(strong("bold"))
code("code")
bold
code
Type [tags$] + [TAB] for a complete list.
There are over 100 available, and if you can't find what you are looking for, you can just pass the HTML as text through:
HTML("<p>Raw html</p>")
includeCSS('raw CSS here')
includeScript('raw JavaScript here')
Place the file in your www/ subdirectory, then reference it with:
# CSS
tags$head(tags$link(rel = "stylesheet",
type = "text/css", href = "<file name>"))
# JavaScript
tags$head(tags$script(src = "<file name>"))
# Images
img(src="<file name>")
runExample("06_tabsets")
Everything for this presentation can be found on my github account.
You can also check out Vadim's, too.