Week 9: parameterized Markdown and Shiny

Robert W. Walker

Overview

Overview

  1. AMA
  2. Parameterized markdown
  3. Shiny

The Assignment [Multi-week]

A shiny

AMA

If there are things you want to figure out with quarto, what are they?

  • I have one example on functions from the blog.

A Discord Post

On programming tidily.

A funny thing about the tidyverse

Quosures. The writeup

Code
mySum <- function(dat, vec) {
    vec <- enquo(vec)
    result <- dat %>%
        summarise(Mean = mean(!!vec, na.rm = TRUE), SD = sd(!!vec, na.rm = TRUE),
            SE = sd(!!vec, na.rm = TRUE)/sqrt(length(!!vec)), P25 = quantile({
                {
                  vec
                }
            }, probs = 0.25), P75 = quantile({
                {
                  vec
                }
            }, probs = 0.75))
    return(result)
}
mtcars %>%
    mySum(., disp)
  Mean  SD   SE P25 P75
1  231 124 21.9 121 326

Shiny

A Mental Model

Ever heard the phrase letting the data speak?

A shiny gives the data a particular set of voices and words.

What do I mean by this?

  • Almost all shiny apps limit the set of things the end-user can do. I know of only one exception to this rule and I use it extensively – radiant.

Something to do….

Watch this!

The Basics

What for?

What in particular?

An Attack Plan

Division of Book

The First Part

  1. A first app
  2. Basic UI
  3. Basic Reactivity
  4. 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?

Let’s Do This…

A First Hit

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…

Inputs

Some things will require specials, e.g. DT and plotly

Outputs

That’s just the beginning

There are an amazing array of extensions.

Reactive Programming

Do this if that. :::: {.columns} ::: {.column width=“50%”}

Insight 1

::: ::: {.column width=“50%”}

Insight 2

::: ::::

The big idea

Reactive

A Graph

## The Example
<iframe src="https://mastering-shiny.org/basic-reactivity.html#the-motivation" width=90% height=90%></frame>

Details

Details

Input/Output

An Important Section

Observers

The low-down

Observers Reconstruct

A Case Study

Is here

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

  1. Parameterized markdown and CRON jobs
    • What is a CRON job?
  2. CRON jobs and web servers?

The Path Forward