Written by Hadley Wickham himself
Slides paraphrase most of the fundamental information
Provided UI functions that automatically wrap HTML, CSS and javascript for common tasks:
Reactive Programming paradigm that tracks dependencies in code and inputs to only re-run the minimal amount of code in order to update outputs
Natively separates UI (frontend) and server (backend) for readability and ease of use
Inputs allow us to interact with the app and specify different values
All inputs have the same first argument: inputId
inputId can be accessed by the server function using input$inputIdThe second argument is typically a label which describes the input in the UI
inputId will be used internally and the label shown externally in the UIRadio Buttons
Selection Dropdown
Text Boxes
# Function structure
textInput("ID", "IDLabel", additional arguments)
# Function example
ui <- fluidPage(
textInput("text", "Enter your text here")
)
# Function structure
selectInput("ID", "IDLabel", additional arguments)
# Function example
c <- ("Massachussets", "New York", "Maryland")
ui <- fluidPage(
selectInput("state", "Choose your state", states)
)
# Outputs
Outputs in the UI are elements created by the server function
The first argument in most output functions is the ID
output$outputIdui <- fluidPage(
textOutput("text")
)
server <- function(input, output, session) {
output$text <- renderText( "Hello world!")
ui <- fluidPage(
tableOutput("table")
)
server <- function(input, output, session) {
output$table <- renderTable(head(mtcars))
}
ui <- fluidPage(
plotOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderPlot(
ggplot(mtcars, x=mpg, y=hp) + geom_point()
)
}
Outputs will be in the UI and render functions in the server
tableOutput() and renderTable()
plotOutput() and renderPlot()
textOutput() and renderText()
# Function example
states <-c("Massachussets", "New York", "Maryland")
ui <- fluidPage(
selectInput("state", "Choose your state", states),
textOutput("selected")
)
server <- function(input, output, session) {
output$selected <- renderText(input$state)
}
fluidPage(
titlePanel(
# app title/description
),
sidebarLayout(
sidebarPanel(
# inputs
),
mainPanel(
# outputs
)
)
)
fluidPage(
fluidRow(
column(4,
...
),
column(8,
...
)
),
fluidRow(
column(6,
...
),
column(6,
...
)
)
)
# Load libraries
library('shiny')
library('tidyverse')
# UI layout with a sidebar and mainpanel
# plotOutput contains the information from output$plot
ui <- fluidPage(
titlePanel("mtcars Plot"),
sidebarLayout(
sidebarPanel("UI Elements could go here"
),
mainPanel(
plotOutput("plot")
)
)
)
# renderPlot() creates the output sent to output$plot by calling ggplot
server <- function(input, output, session) {
output$plot <- renderPlot(
ggplot(mtcars, aes(x=mpg, y=hp)) + geom_point()
)
}
shinyApp(ui = ui, server = server)
Reactive expressions and outputs will only change if and only if their inputs change
Laziness means code will only be re-run if needed
Reactivity can be used to control the flow of data and actions through your application
render* functions are reactive expressions
You can create your own by wrapping your function with reactive({})
Reactive expressions cache their results and only return a new result if it detects any changes in dependencies
Render* + built-in input selections are a simple example of reactivity