Welcome and Getting Started

Welcome to the workshop and hello, bslib!

View slides in full screen

Outline

πŸ§‘β€πŸ’» Your Turn

exercises/01_app.R

exercises/01_app.R
library(shiny)
library(bslib)
library(dplyr)
library(ggplot2)

library(collegeScorecard)
# Provides: `scorecard` and `school`

# Data --------------------------------------------------------------------
scorecard_recent <-
  scorecard |>
  filter(academic_year == max(academic_year)) |>
  select(id, n_undergrads, cost_avg, rate_admissions, rate_completion)

school <- left_join(school, scorecard_recent, by = "id")

range_slider <- function(data, column, label, by = 15000, step = by) {
  val_range <- range(data[[column]], na.rm = TRUE)
  val_range[1] <- floor(val_range[1] / by) * by
  val_range[2] <- ceiling(val_range[2] / by) * by

  sliderInput(
    inputId = column,
    label = label,
    value = val_range,
    min = val_range[1],
    max = val_range[2],
    step = step,
    ticks = FALSE
  )
}

# Inputs ------------------------------------------------------------------
input_var <-
  varSelectInput(
    inputId = "var",
    label = "School Variable",
    school |> select(where(\(x) length(unique(x)) < 60))
  )

input_n_undergrads <-
  range_slider(
    school,
    "n_undergrads",
    "Number of Undergrad Students",
    by = 15000,
    step = 5000
  )

input_cost_avg <-
  range_slider(
    school,
    "cost_avg",
    "Average Yearly Cost",
    by = 2500
  )

# UI ----------------------------------------------------------------------

ui <- fluidPage(
  titlePanel("01 - Hello bslib!"),
  sidebarLayout(
    sidebarPanel(
      input_var,
      input_n_undergrads,
      input_cost_avg
    ),
    mainPanel(
      plotOutput("plot", height = 700)
    )
  )
)


# Server ------------------------------------------------------------------

server <- function(input, output, session) {
  output$plot <- renderPlot({
    school |>
      filter(
        between(n_undergrads, input$n_undergrads[1], input$n_undergrads[2]),
        between(cost_avg, input$cost_avg[1], input$cost_avg[2])
      ) |>
      ggplot() +
      aes(y = !!input$var) +
      geom_bar() +
      labs(title = input$var, y = NULL) +
      theme_minimal(16)
  })
}

shinyApp(ui, server)

Are we all set up? Open the app in exercises/01_app.R. Run the app to make sure it works. Can everyone in your breakout session run the app? Help each other make sure you’re set up.

Take a Shiny app and make it bslib-ier Replace fluidPage() with page_fluid(). What other page types are available? Try out some Bootswatch themes. For bonus points, try setting a theme-related Sass variable.

Use the app to learn about the variables in school.

Follow up

snippet shinyapp
    library(shiny)
    library(bslib)

    ui <- page_${1:sidebar}(
      ${0}
    )

    server <- function(input, output, session) {

    }

    shinyApp(ui, server)