Welcome and Getting Started
Welcome to the workshop and hello, bslib!
Outline
- Welcome to R/Medicine 2024!
- What is bslib?
- Breakout activity: Are we all set up?
- Follow up
π§βπ» Your Turn
exercises/01_app.R
Exercise Code
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)
<- left_join(school, scorecard_recent, by = "id")
school
<- function(data, column, label, by = 15000, step = by) {
range_slider <- range(data[[column]], na.rm = TRUE)
val_range 1] <- floor(val_range[1] / by) * by
val_range[2] <- ceiling(val_range[2] / by) * by
val_range[
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",
|> select(where(\(x) length(unique(x)) < 60))
school
)
<-
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 ----------------------------------------------------------------------
<- fluidPage(
ui titlePanel("01 - Hello bslib!"),
sidebarLayout(
sidebarPanel(
input_var,
input_n_undergrads,
input_cost_avg
),mainPanel(
plotOutput("plot", height = 700)
)
)
)
# Server ------------------------------------------------------------------
<- function(input, output, session) {
server $plot <- renderPlot({
output|>
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
- Check in
- Bonus: new
shinyapp
snippet
snippet shinyapp
library(shiny)
library(bslib)
ui <- page_${1:sidebar}(
${0}
)
server <- function(input, output, session) {
}
shinyApp(ui, server)