1 of 61

Shiny Apps for Communicating Science

Jake Lawlor

jakelawlor.github.io

repo: https://github.com/jakelawlor/Shiny_App_Tutorial

2 of 61

Contents

  1. Shiny Apps Overview
    1. What Are Shiny Apps?
    2. Why?
  2. App Anatomy: the basics
    • Server
    • User Interface
  3. Shiny affiliates
    • interactivity
    • report generation
  4. Sharing Shiny Apps
  5. Catwalk

3 of 61

Shiny Apps Overview

4 of 61

What are Shiny Apps?

5 of 61

What are Shiny Apps?

  • Interactive web-based frameworks for R
  • Can be local apps, stand-alone webpages, or embedded in .rmd docs

6 of 61

Why?

7 of 61

Why?

  • Personal use
    • data exploration
    • simulations
  • sharing results
    • communicating with stakeholders
    • allowing viewers to access full datasets
    • custom report generation

8 of 61

Shiny App Anatomy

the basics

9 of 61

Shiny App Anatomy

the basics

User Interface

(UI)

Server

10 of 61

Shiny App Anatomy

the basics

User Interface

(UI)

Server

inputs

outputs

11 of 61

Shiny App Anatomy

the basics

User Interface

(UI)

Server

inputs

outputs

“front-end”

“back-end”

lists

12 of 61

app.R

ui <- fluidPage( �� widgetInput(“id”),

somethingOutput(“id”)

)

server <- function(input, output){ �

dataset <- reactive(input$id)�� output$id <- renderSomething()

}

13 of 61

app.R

ui <- fluidPage( �� widgetInput(“id”),

somethingOutput(“id”)

)

server <- function(input, output){ �

dataset <- reactive(input$id)�� output$id <- renderSomething()

}

14 of 61

app.R

ui <- fluidPage( �� widgetInput(“id”),

somethingOutput(“id”)

)

server <- function(input, output){ �

dataset <- reactive(input$id)�� output$id <- renderSomething()

}

15 of 61

app.R

ui <- fluidPage( �� sliderInput(“years”),

plotOutput(“plot”)

)

server <- function(input, output){ �

dataset <- reactive(input$years)�� output$plot <- renderPlot(dataset)

}

16 of 61

Shiny App Anatomy - server

17 of 61

Shiny App Anatomy - server

similar to R scripting as you know it, but with reactivity

18 of 61

Shiny App Anatomy - server

similar to R scripting as you know it, but with reactivity

reactivity allows inputs and outputs to talk to each other

19 of 61

Shiny App Anatomy - server

similar to R scripting as you know it, but with reactivity

reactivity allows inputs and outputs to talk to each other

requires comfort in functional programming

20 of 61

Shiny App Anatomy - server

reactivity

21 of 61

Shiny App Anatomy - server

reactivity

“downstream” features automatically adapt to “upstream” changes

can respond to specific cues, or ambient changes in environment

22 of 61

Shiny App Anatomy - server

reactivity

input

output

23 of 61

Shiny App Anatomy - server

reactivity

input

output

input

24 of 61

Shiny App Anatomy - server

reactivity

input

output

input

output

25 of 61

Shiny App Anatomy - server

reactivity

input

reactive

output

input

output

26 of 61

Shiny App Anatomy - server

reactivity

input

reactive

output

input

output

output

27 of 61

reactivity ✨, an example:

input

output

input$selected_islands <-

checkboxGroupInput(...)

output$plot <- renderPlot({

filtered_data <- data %>%

filter(island %in% input$selected_islands)

ggplot(filtered_data)

})

28 of 61

reactivity ✨, an example:

input

output

input$selected_islands <-

checkboxGroupInput(...)

output$plot <- renderPlot({

filtered_data <- data %>%

filter(island %in% input$selected_islands)

ggplot(filtered_data)

})

each time input is changed, R is doing 2 things:

  1. filter plotdata
  2. makeplot

29 of 61

reactivity ✨, an example:

input

output

input$selected_islands <-

checkboxGroupInput(...)

output$plot <- renderPlot({

plotdata <- data %>%

filter(island %in% input$selected_islands)

ggplot(plotdata)

})

output 2

output$table <- rendertable({

tabledata <- data %>%

filter(island %in% input$selected_islands)

table(tabledata)

})

30 of 61

reactivity ✨, an example:

input

output

input$selected_islands <-

checkboxGroupInput(...)

output$plot <- renderPlot({

plotdata <- data %>%

filter(island %in% input$selected_islands)

ggplot(plotdata)

})

output 2

output$table <- rendertable({

tabledata <- data %>%

filter(island %in% input$selected_islands)

table(tabledata)

})

each time input is changed, R is doing 4 things:

  • filter plotdata
  • makeplot
  • filter tabledata
  • make table

31 of 61

reactivity ✨, an example:

input

output

input$selected_islands <-

checkboxGroupInput(...)

output$plot <- renderPlot({

ggplot(reactivedata()))

})

output 2

output$table <- rendertable({

table(reactivedata())

})

reactive

reactivedata <- reactive({

data %>%

filter(island %in%

input$selected_islands)

})

32 of 61

reactivity ✨, an example:

input

output

input$selected_islands <-

checkboxGroupInput(...)

output$plot <- renderPlot({

ggplot(reactivedata()))

})

output 2

output$table <- rendertable({

table(reactivedata())

})

reactive

reactivedata <- reactive({

data %>%

filter(island %in%

input$selected_islands)

})

using reactives limits operations:

  • make reactive data
  • make plot with reactive data
  • make table with reactive data

33 of 61

Shiny App Anatomy - server

reactivity

Type of Reactivity

Functions

Common uses

reactive object

reactive(), reactiveVal(), reactiveValues(), render___()

continuously updating datasets based on user input

reactive action

observe()

completing a task, rather than creating an object (does not yield output)

response

observeEvent(), eventReactive()

responses to action buttons,

34 of 61

Shiny App Anatomy - ui

35 of 61

Shiny App Anatomy - ui

  • User Interface / “front-end”
  • part of app that interacts with user
  • good defaults, but also customizable
  • R -> HTML

36 of 61

Shiny App Anatomy - ui

  • Widgets

Widgets allow users to interact with apps!

37 of 61

Shiny App Anatomy - ui

  • Default shiny layout

38 of 61

Shiny App Anatomy - ui

  • Default shiny layout
    • title panel
    • sidebar panel
    • main panel

sidebar panel

main panel

title panel

39 of 61

Shiny App Anatomy - ui

  • Shiny Dashboard layout

40 of 61

Shiny App Anatomy - ui

  • Shiny Dashboard layout
    • dashboard header
    • dashboard sidebar
    • dashboard body
      • boxes
      • rows
      • columns

dashboard sidebar

dashboard body

dashboard header

box

41 of 61

Shiny App Anatomy - ui

  • HTML Layout

42 of 61

Shiny App Anatomy - ui

  • HTML Layout
    • fluidRows
    • columns
    • divs
    • css for styling
    • HTML and js code embedded when necessary

43 of 61

Shiny App Anatomy - folder structure

.Rproj

App1

app.R

App2

app.R

App3

ui.R

server.R

App4

app.R

www

style.css

data.rds

other extras

44 of 61

45 of 61

Shiny affiliates for full interactivity

46 of 61

Shiny affiliates for full interactivity

You may know:

  • Static plots
    • ggplot2, base plot

You may benefit from:

  • Plots with tooltips
    • plotly, ggplotly, plot_ly

47 of 61

Shiny affiliates for full interactivity

You may know:

  • Static plots
    • ggplot2, base plot
  • Static maps
    • sf, ggplot, GIS

You may benefit from:

  • Plots with tooltips
    • plotly, ggplotly, plot_ly
  • Interactive maps
    • leaflet

48 of 61

Shiny affiliates for full interactivity

You may know:

  • Static plots
    • ggplot2, base plot
  • Static maps
    • sf, ggplot, GIS
  • Static tables
    • gt, tibble

You may benefit from:

  • Plots with tooltips
    • plotly, ggplotly, plot_ly
  • Interactive maps
    • leaflet
  • Interactive tables
    • DT, reactable

49 of 61

Shiny affiliates for full interactivity

You may know:

  • Static plots
    • ggplot2, base plot
  • Static maps
    • sf, ggplot, GIS
  • Static tables
    • gt, tibble

You may benefit from:

  • Plots with tooltips
    • plotly, ggplotly, plot_ly
  • Interactive maps
    • leaflet
  • Interactive tables
    • DT, reactable

See app_8_upgrade_your_skills for examples

50 of 61

Shiny for communication

51 of 61

Shiny for communication

interactivity allows users to view datasets more closely than they otherwise could

52 of 61

Shiny for communication

interactivity allows users to view datasets more closely than they otherwise could

user-generated report generation through Parameterized R Markdowns

(sending shiny inputs and reactives to embedded rmd scripts).

53 of 61

Shiny for communication

interactivity allows users to view datasets more closely than they otherwise could

user-generated report generation through Parameterized R Markdowns

(sending shiny inputs and reactives to embedded rmd scripts).

see example 5: https://github.com/jakelawlor/Shiny_App_Tutorial

54 of 61

Deploying Shiny Apps

55 of 61

Deploying Shiny Apps

  • Local shares through git repositories
    • Requires users have R, shiny, and all necessary packages

56 of 61

Deploying Shiny Apps

  • Local shares through git repositories
    • Requires users have R, shiny, and all necessary packages
  • Deploy to the Cloud
    • shinyapps.io
    • Free or paid versions

57 of 61

Deploying Shiny Apps

  • Local shares through git repositories
    • Requires users have R, shiny, and all necessary packages
  • Deploy to the Cloud
    • shinyapps.io
    • Free or paid versions
  • Deploy on a server
    • RStudio connect
    • company/university server space

58 of 61

Deploying Shiny Apps

  • Local shares through git repositories
    • Requires users have R, shiny, and all necessary packages
  • Deploy to the Cloud
    • shinyapps.io
    • Free or paid versions
  • Deploy on a server
    • RStudio connect
    • company/university server space

as files

as urls

59 of 61

in summary, shiny apps offer:

60 of 61

in summary, shiny apps offer:

web development without actually having to know web development

communication directly to collaborators / stakeholders

customized data viewing and report generation

61 of 61

questions?

catwalk?