Week 11: Shiny Part III

Author

Robert W. Walker

Published

April 3, 2023

Meeting Date: April 3, 2023.

Last updated: 2023-04-10 13:53:29

Timezone: America/Los_Angeles

Class Plan

  1. AMA
  2. Shiny Overview
  3. Shiny, Part III

Slides:

Week 11 Slides

Homework

The ninth assignment is rolled into the creation of a shiny application.

Syllabus Module for Week 11

Unifying it All Together with Shiny

Working through these chapters

Start with:

library(shiny)
library(dplyr)
ui <- ...
server <- ...
shinyApp(ui, server)

and the steps will fill in a ui and server

Chapter 12

  • tidy evaluation

Screen shot

  • Passing variable names
library(shiny)
library(dplyr)
ui <- fluidPage(
  selectInput("var", "Sort by", choices = names(mtcars)),
  checkboxInput("desc", "Descending order?"),
  tableOutput("data")
)
server <- function(input, output, session) {
  sorted <- reactive({
    if (input$desc) {
      arrange(mtcars, desc(.data[[input$var]]))
    } else {
      arrange(mtcars, .data[[input$var]])
    }
  })
  output$data <- renderTable(sorted())
}
shinyApp(ui, server)
  • The importance of req. What does it do?

Shot of help

Chapter 13

Why reactivity?

Shot

Playing with Reactive

Simple Example

Chapter 14

The reactive graph is worth playing with. Due to modern defaults in RStudio, the workflow is a bit tricky. We will have to run the app in the console.

Settings

Run it and close it to get this.

reactlog

Chapter 15

Reactives, single and plural

Two types - An oddity

Behavior is a bit different

Pay very close attention to 15.3 on observe versus observeEvent. The example in 16.3.3…

library(shiny)
reactiveConsole(TRUE)
x <- reactiveVal(1)
y <- observe({
  x()
  observe(print(x()))
})
x(2)
x(3)
x(2)
  • On Isolate

Details

Isolate is not all that common because the behavior is embedded in two common tools:

observeEvent and eventReactive

15.5 on timing

part 1 - invalidateLater

part 2 - Details

15.5.2 and the importance of timing. Using on.exit

Chapter 16: Escaping the Graph

Video

The Case Studies

Part 1

Part 2

Two that are related.

Part 3

Part 4

The timer

Timer

Warnings:

It is quite easy to get carried away and muddle the flow.

Anti-patterns

Readings:

References

Code
knitr::write_bib(names(sessionInfo()$otherPkgs), file="bibliography.bib")

References

Chang, Winston, Joe Cheng, JJ Allaire, Carson Sievert, Barret Schloerke, Yihui Xie, Jeff Allen, Jonathan McPherson, Alan Dipert, and Barbara Borges. 2022. Shiny: Web Application Framework for r. https://shiny.rstudio.com/.