The First Part
- A first app
- Basic UI
- Basic Reactivity
- Case Study: ER Injuries
UI and Server
There are multiple ways to devise a shiny app but it ultimately comes down to a UI – user interface – and a set of server instructions driven by the UI.
Shiny uses reactive programming to automatically update outputs when inputs change so we’ll finish off the chapter by learning the third important component of Shiny apps: reactive expressions ## Stepping Back, What could be done?
A Running Example
library(shiny)
ui <- fluidPage(
"Hello, world!"
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
Adding to UI
ui <- fluidPage(
selectInput("dataset", label = "Dataset", choices = ls("package:datasets")),
verbatimTextOutput("summary"),
tableOutput("table")
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
Adding to Server
ui <- fluidPage(
selectInput("dataset", label = "Dataset", choices = ls("package:datasets")),
verbatimTextOutput("summary"),
tableOutput("table")
)
server <- function(input, output, session) {
output$summary <- renderPrint({
# Note the duplication
dataset <- get(input$dataset, "package:datasets")
summary(dataset)
})
output$table <- renderTable({
# Note the duplication
dataset <- get(input$dataset, "package:datasets")
dataset
})
}
shinyApp(ui, server)
Adding to Server 2
ui <- fluidPage(
selectInput("dataset", label = "Dataset", choices = ls("package:datasets")),
verbatimTextOutput("summary"),
tableOutput("table")
)
server <- function(input, output, session) {
# Create a reactive expression
dataset <- reactive({
get(input$dataset, "package:datasets")
})
output$summary <- renderPrint({
# Use a reactive expression by calling it like a function
summary(dataset())
})
output$table <- renderTable({
dataset()
})
}
shinyApp(ui, server)
Making this more rich…
Some things will require specials, e.g. DT and plotly
Reactive Programming
Do this if that. :::: {.columns} ::: {.column width=“50%”}
::: ::: {.column width=“50%”}
::: ::::
The big idea
## The Example
<iframe src="https://mastering-shiny.org/basic-reactivity.html#the-motivation" width=90% height=90%></frame>
An Important Section
Observers
The low-down
Observers Reconstruct
Other Use Cases
The blog highlights the need for special programming tools if we decide that our use cases is a model choice
approach.
Some Alternatives to Consider
- Parameterized markdown and CRON jobs
- CRON jobs and web servers?