+ - 0:00:00
Notes for current slide
Notes for next slide

Shiny Part 1: Build apps


FISH 549

Megsie Siple

NOAA AFSC - Groundfish Assessment Program

2024-03-03 (updated: 2025-03-03)

1 / 31

Hello!

2 / 31

Introductions

I am Margaret Siple (I go by Megsie) and I work at NOAA's Alaska Fisheries Science Center in the Groundfish Assessment Program. I live in Beacon Hill with two cats, Molly and Loki.

molly

kiki

3 / 31

Introductions

Here is a brief poll so I can get to know you.

qrcodeforpoll

4 / 31

Work and research

5 / 31

A note about public resources in this presentation

Many components of this work were made possible by public resources, including 18F.

6 / 31

Why I learned Shiny

dolphins

Image: Jeff Moore

7 / 31

Why I learned Shiny

mmbiet

8 / 31

My team at AFSC uses many FISH 549 skills

FISH 549 learning goals

  • Using GitHub

  • Documenting data

  • Using SQL to query databases

  • Writing unit tests

  • Creating packages and writing unit tests

  • Using Markdown to create documentation

GAP Activities

✅ We use GH daily, including to plan survey deployment

Documenting data is the story of our lives

✅We store bottom trawl survey data in a SQL database

✅Interior and public packages use unit tests and CI (e.g., coldpool)

✅We use Markdown for software dx, reproducible reports(Bering Sea, Gulf of Alaska/Aleutian Islands), and more

9 / 31

os2 Photo: Shaya Lyon

10 / 31

Applied fisheries management can be like playing music

Each musician has areas where they’re very specialized, and areas where they are clueless

11 / 31

Applied fisheries management can be like playing music

Each musician has areas where they’re very specialized, and areas where they are clueless

What we do is a combination of highly technical skilled work, creativity and multitasking

11 / 31

Applied fisheries management can be like playing music

Each musician has areas where they’re very specialized, and areas where they are clueless

What we do is a combination of highly technical skilled work, creativity and multitasking

We’re all working together to try to make a big, beautiful thing happen

11 / 31

To successfully provide scientific advice for fisheries management, we need many components to work together:

1) Best available science (+ reproducibility)

2) Well-defined pathways for implementing advice

3) Trust and communication between researchers and stakeholders

12 / 31

To successfully provide scientific advice for fisheries management, we need many components to work together:

1) Best available science (+ reproducibility)

2) Well-defined pathways for implementing advice

3) Trust and communication between researchers and stakeholders

Shiny can help us do these things and create something beautiful that communicates data and models in a new way.

12 / 31

Outline

  1. shiny basics - when to use it, what it's good for
13 / 31

Outline

  1. shiny basics - when to use it, what it's good for

  2. How to make an app

13 / 31

Outline

  1. shiny basics - when to use it, what it's good for

  2. How to make an app

  3. Lessons learned from developing a Shiny app for management advice

13 / 31

Outline

  1. shiny basics - when to use it, what it's good for

  2. How to make an app

  3. Lessons learned from developing a Shiny app for management advice

If you want practice or you learn well from hands-on examples, check out https://github.com/mcsiple/shinyoverview

13 / 31

Outline

  1. shiny basics - when to use it, what it's good for

  2. How to make an app

  3. Lessons learned from developing a Shiny app for management advice

If you want practice or you learn well from hands-on examples, check out https://github.com/mcsiple/shinyoverview

There are many other things available in the repo that we won't cover today, but examples are included so you can try them on your own:

  1. Live-translating your Shiny apps into other languages using {shiny.18n}

  2. Using Shiny to teach remotely with {learnr}

  3. Generating Markdown reports from Shiny

13 / 31

What is Shiny?

whatisshiny

14 / 31

When is Shiny useful?

When we want to make R code accessible outside of an R or RStudio environment. For example,

15 / 31

When is Shiny useful?

When we want to make R code accessible outside of an R or RStudio environment. For example,

  • teaching people how to use a package
15 / 31

When is Shiny useful?

When we want to make R code accessible outside of an R or RStudio environment. For example,

  • teaching people how to use a package

  • giving non-experts a way to "get to know" models and data

15 / 31

When is Shiny useful?

When we want to make R code accessible outside of an R or RStudio environment. For example,

  • teaching people how to use a package

  • giving non-experts a way to "get to know" models and data

  • streamlining certain code-intensive activities (like summarizing and communicating simulation outputs)

15 / 31

Shiny can be a little gnarly at first.

stinkspirit

16 / 31

Starting a new Shiny app

Install shiny:

install.packages("shiny")

From The RStudio IDE, pick New File -> Shiny Web App

You can choose between single (app.R) or multiple files (ui.R and server.R)

Does it matter which one you choose?

Not really. Some like the organization of having multiple files 🤷

poll

17 / 31

Each Shiny app has two components

The ui (user interface) object dictates the appearance of the app. For something to appear in your app, it needs to be in the UI. UI functions write HTML.

If you put in the console:

shiny::titlePanel("Here is a title")

It returns this html:

<h2>Old Faithful Geyser Data</h2>
18 / 31

Each Shiny app has two components

The ui (user interface) object dictates the appearance of the app. For something to appear in your app, it needs to be in the UI. UI functions write HTML.

If you put in the console:

shiny::titlePanel("Here is a title")

It returns this html:

<h2>Old Faithful Geyser Data</h2>

The server() function contains rendering expressions that create the objects to display.

The server function and UI object are passed to the shinyApp() function to create a Shiny app object.

19 / 31

ui.R

The ui looks like this (the "interactive" part is highlighted):

ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)),
mainPanel(
plotOutput("distPlot"))
)
)

The corresponding ui looks like this:

basicui

20 / 31

ui.R

The ui code contains the following information:

  1. The UI type

    • fluidPage() puts elements in rows that can include columns 🍰
    • navbarPage() has a navigation bar 📁
  2. Layout elements (sidebarLayout() etc.)

  3. Theme information (e.g., {shinythemes})

21 / 31

ui.R

The ui code contains the following information:

  1. The UI type

    • fluidPage() puts elements in rows that can include columns 🍰
    • navbarPage() has a navigation bar 📁
  2. Layout elements (sidebarLayout() etc.)

  3. Theme information (e.g., {shinythemes})

  4. Output objects (plotOutput(), etc.)

  5. Input objects (sliderInput(),fileInput() etc.) - also called "widgets"

Input objects link the UI to R code that runs on the server.

21 / 31

server.R

The server builds a list-like object called output. The contents of output can be displayed in the ui.

server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
22 / 31

ui.R/server.R

The server builds a list-like object called output. output objects are displayed in the ui. Here is how they are linked:

server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)),
mainPanel(
plotOutput("distPlot"))
)
)
23 / 31

What's on the server?

R code... mostly 😜

24 / 31

What's on the server?

R code... mostly 😜

spirited

24 / 31

server.R

  • Rendering functions (renderPlot(), renderTable(), etc.)
    • Build a new object to display every time the inputs change
25 / 31

server.R

  • Rendering functions (renderPlot(), renderTable(), etc.)

    • Build a new object to display every time the inputs change
  • Reactive expressions are "lazy" - they don't execute unless they are specifically called to do something.

    • reactive() caches reactive objects so you can access them later in the server logic -- very important!
    • eventReactive() creates reactive objects but only when a specific input changes (e.g., a "Fit this model!" button is clicked)
25 / 31

server.R

  • Rendering functions (renderPlot(), renderTable(), etc.)

    • Build a new object to display every time the inputs change
  • Reactive expressions are "lazy" - they don't execute unless they are specifically called to do something.

    • reactive() caches reactive objects so you can access them later in the server logic -- very important!
    • eventReactive() creates reactive objects but only when a specific input changes (e.g., a "Fit this model!" button is clicked)
  • Observe expressions are "eager" - they automatically execute when their dependencies change. Expressions like observe() can:

    • autopopulate default values in a form
    • change the range for one input based on another input
25 / 31

lec_25_26_shiny_notes.Rmd

26 / 31

Lessons learned from developing mmrefpoints and its Shiny tool

droplets

27 / 31

The long (and rewarding!) path

  • The Ocean Modeling Forum's request: a free, online app that would allow stakeholders to do a scenario analysis for a marine mammal stock of their choice
  • International target audience
  • Year 1: Consultation with scientists; building and testing the population dynamics model
  • Year 1: Consultation with international stakeholders interested in the tool
  • Year 2: Show app to actual web developers (humbling 😆)
  • Year 2: Testing with students, stakeholders, other scientists, other developers
  • Year 3(?): Publish R package and app in JOSS; present finished product
28 / 31

UI is worth your time

Shiny defaults are not the most intuitive / appealing / accessible version they can be.

29 / 31

UI is worth your time

Shiny defaults are not the most intuitive / appealing / accessible version they can be.

If you are designing an app for management, a good UI is essential.

29 / 31

UI is worth your time

Shiny defaults are not the most intuitive / appealing / accessible version they can be.

If you are designing an app for management, a good UI is essential.

  • my #1 tip: if you have time, pilot test with subject matter experts AND users
29 / 31

UI is worth your time

Shiny defaults are not the most intuitive / appealing / accessible version they can be.

If you are designing an app for management, a good UI is essential.

  • my #1 tip: if you have time, pilot test with subject matter experts AND users

  • use UX resources if they are available!

29 / 31

UI is worth your time

Shiny defaults are not the most intuitive / appealing / accessible version they can be.

If you are designing an app for management, a good UI is essential.

  • my #1 tip: if you have time, pilot test with subject matter experts AND users

  • use UX resources if they are available!

  • if your institution doesn't have UX resources, design pilot testing so that you get helpful feedback on UX:

UX = user experience.

29 / 31

Quick poll

What material would you like to cover on Wednesday? Poll here

30 / 31

Fin!

contact

📧: margaret.siple@noaa.gov ☁️: @mcsiple on BlueSky

More Shiny resources:

Mastering Shiny by Hadley Thee Wickham

Colin Fay has several talks on Shiny app workflow and production

We love a cheatsheet

Today's code treat 🧋

Put emoji in your GitHub commits by using the :: syntax, e.g., git commit -m this code is still growing :seedling: Emoji options at gitmoji.dev


Slides created using the R package xaringan.

31 / 31

Hello!

2 / 31
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow