Interactivity Continued

HES 505 Fall 2022: Session 27

Matt Williamson

Objectives

By the end of today you should be able to:

  • Build simple interactive graphics with ggplot and plotly

  • Describe the basic structure of shiny apps

  • Begin incorporating interactive elements into stories

Clarity in presentation (revisited)

Using plotly

g <- txhousing %>% 
  # group by city
  group_by(city) %>%
  # initiate a plotly object with date on x and median on y
   plotly::plot_ly(x = ~date, y = ~median) %>%
  # add a line plot for all texan cities
   plotly::add_lines(name = "Texan Cities", hoverinfo = "none", 
            type = "scatter", mode = "lines", 
            line = list(color = 'rgba(192,192,192,0.4)')) %>%
  # plot separate lines for Dallas and Houston
   plotly::add_lines(name = ~city, 
            data = filter(txhousing, 
                          city %in% c("Dallas", "Houston")),
            hoverinfo = ~city,
            line = list(color = c("red", "blue")),
            color = ~city)

Using plotly

  • Syntax is similar to ggplot

  • hoverinfo describes which elements you’d like to make interactive

  • Other plot elements available (see ?plot_ly)

Interactive maps with mapview and tmap

  • Easy extension of your existing

  • Class Demo

Other options

  • leaflet

  • mapview

  • mapsapi

Animated maps with tmap and gganimate

urb_anim = tm_shape(world) + tm_polygons() + 
  tm_shape(urban_agglomerations) + tm_dots(size = "population_millions") +
  tm_facets(along = "year", free.coords = FALSE)
tmap_animation(urb_anim, filename = "urb_anim.gif", delay = 25)

dynamic

Shiny

What is shiny

  • An Rstudio product designed to allow reactive data visualizations via an R server

  • Allows users to generate new results (not just plot your old stuff)

The simplest shiny app

server <- function(input, output, session) { } #the server

ui <- basicPage("This is a real Shiny app") # the user interface

shinyApp(ui = ui, server = server) # this launches your app

Shiny applications not supported in static R Markdown documents

Reactivity

  • Flexdashboards allow the user to visualized data you’ve created

  • Integrating shiny apps allows them to run new analysis!

  • Examples

  • Reactivity is the link between user input and server output

Reactivity

server <- function(input, output, session) {
  
  observe({
    # even though the slider is not involved in a calculation, if
    # you change the slider it will run all this code and update the text box
    # changes to the mytext box also will trigger the code to run
    input$myslider
    txt <- paste(input$mytext, sample(1:10000, 1))
    updateTextInput(session, inputId = "myresults", value = txt)  
    
  })
  
}

ui <- basicPage(
  h3("The results text box gets updated if you change the other text box OR the slider."),
  sliderInput("myslider", "A slider:", min=0, max=1000, value=500),
  textInput("mytext", "Input goes here", value = "Initial value"),
  textInput("myresults", "Results will be printed here")
)
shinyApp(ui = ui, server = server)

Shiny applications not supported in static R Markdown documents

Flexdashboards and storymaps

The Storymap

Telling stories with flexdashboards

  • Dashboard: a visual display of interrelated data

  • flexdashboards are an Rmarkdown-powered way to build dashboards

  • Integrates with plotly, leaflet and other htmlwidgets

  • Examples ]

Telling stories with flexdashboards

  • Audience engagement drives design

  • “Scrolling” layouts allow linear story telling (like the ArcGIS Storymap)

  • “Tabset” layouts allow users to construct their own stories

  • Lots of readymade templates, themes, and add-ins

  • Display your research