INTERACTIVE DASHBOARDS WITH SHINY

Robert Mitchell | @robertmitchellv
2018-12-10

ABOUT ME

img

  • I work at Skid Row Housing Trust as a data analyst
  • (I'll just refer to it as SRHT from now on)
  • I have been working in this role for around three years
  • I am self taught as a programmer/analyst
  • Background in philosophy, comp lit, french, information science

A BIT ABOUT SRHT

img

  • We do three things:
    1. Develop affordable housing; specifically permanent supportive housing
    2. Provide supportive services to the residents of our affordable housing projects
    3. Have an affiliate manage the properties, repairs, et cetera

A BIT ABOUT SRHT

img

  • We believe in the Housing First model
  • We believe in Harm Reduction
  • This is pretty much industry standard for the most part

WHAT I WANT TO COVER

  • UI ➡️ server ➡️ user feedback loop
  • The different ways your shiny app can be interactive
  • The different ways your shiny app can look
    • The libraries associated with these efforts
  • How you can dip your toe into this kind work if phrases like deploy my application are scary using flexdashboard instead of shiny
  • Briefly touch on the improvements to the shiny framework, e.g., async and how you can make it scale (things to think about for later)

WHO THIS TALK IS FOR

  • People who have been learning R for a little while and are curious about they can put their work online
  • You've maybe done a shiny tutorial before but didn't finish it

***T

img

WHO THIS TALK IS FOR

img

  • You're eager to learn how some of this works but may not have time to put it to use yet
  • You're really just dipping your toe into this and want to get an idea into how it works

WHAT I HOPE FOR YOU AFTER

  • It's ok if you walk away from this still not sure how you're going to build your shiny application–writing code is hard and you should know it takes non-software engineers a while to wrap their minds around things like this
  • This is definitely my experience

img

WHAT I HOPE FOR YOU AFTER

img

  • The confidence to go home, read through some documentation and just try to start putting things together!
  • You won't learn until you try!

WHY SHINY?


img

WHY SHINY?

  • You can leverage the work you have already done in R to accomplish something you would normally need to learn JavaScript, Ruby, Python, PHP, or something else to accomplish
  • The same way that you would create a visualization for an rmarkdown report can be repurposed for another medium
    • This is especially true of flexdashboard, which you should always use in the event a shiny application is unnecessarily adding complexity to your work! (we'll talk about this more later)

FEEDBACK LOOP?

  • I'm talking about the ways in which your user clicks and explores your dashboard's user interface (UI)
  • Which then triggers code in your server code to run
  • Which then returns output back to your input
  • So your user can get what they need

img

HOW DOES THIS WORK?


The magic behind this is reactivity

WHAT IS REACTIVITY?

reactivity

n. reactivity is how we describe the way in which your shiny application is able to respond to input and update output instantly.

In order for your application to be able to react in this way, it needs to know how to deal with values that will change. We call these reactive values.

HOW DOES REACTIVITY WORK?

  • You don't need to know how it works, in order to use it.
  • This is actually really good news! It's not meant to feel condescending at all.
  • Much better to know how you use reactive values in a shiny application for now.

HOW DOES REACTIVITY WORK?

  • It's hard to make a slide that can help you understand, so we'll run the following code in our R console
  • If you don't have shiny installed, then first:
install.packages("shiny")
  • Load the library
library(shiny)
  • Now run the example
runExample("01_hello")

EXPLORE THE EXAMPLES

  • If you run:
runExample()
  • R will return examples you can explore
runExample
 [1] "01_hello"      "02_text"       "03_reactivity" "04_mpg"       
 [5] "05_sliders"    "06_tabsets"    "07_widgets"    "08_html"      
 [9] "09_upload"     "10_download"   "11_timer"     

LET'S TAKE A STEP BACK

  • Jumping right in to reactivity can make learning shiny seem difficult.
  • Let's start with the basics of the UI

LET'S TAKE A STEP BACK

  • Since we're talking about dashboards, it makes the most sense to introduce you to the shinydashboard package (https://github.com/rstudio/shinydashboard)
  • A basic shinydashboard UI will consist of the following three components
    1. The header dashboardHeader()
    2. The sidebar dashboardSidebar()
    3. The body dashboardBody()

UI COMPONENTS IN CODE

library(shinydashboard)

dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody()
)

A COMPLETE SHINYDASHBOARD

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody()
)

server <- function(input, output) { }

shinyApp(ui, server)
  • Believe it or not, this is a complete + functioning shinydashboard!

WEAVING THE UI TO THE SERVER

Let's return to some of the examples to see how this works.

BE FLEXIBLE

  • It may seem daunting to figure out how you will serve this dashboard to your users–this can be challenging since there are so many different ways you can approach this.
  • However, depending on the consumers of your analysis and their needs you can approach creating dashboards in a slightly different way:
    • You can leverage what you've learned about how shiny works and employ the same strategies in creating a flexdashboard, which doesn't require an R server to run!
    • You can even put it in a share folder and run it in the end user's browser!

BE FLEXIBLE

  • Leverage what you already know about rmarkdown to create dashboards that you can run without a server
  • If you don't need to have sliders, or reactive elements; or if this seems like something you don't want to learn, you can just set shiny aside to focus on flexdashboard first
  • This will help you get more comfortable with developing dashboards.

BE FLEXIBLE

DIFFERENT WAYS TO BE INTERACTIVE

  • Rather than rely on shiny to do all of the reactive/interactive work, you can also use htmlwidgets (https://github.com/ramnathv/htmlwidgets)
    • What are htmlwidgets?
    • plotly can be an htmlwidget
    • leaflet can be included as an htmlwidget

EXAMPLE OF INTERACTIVE PLOTS

ASYNC + FUTURES

  • Since R is a single threaded language (meaning it run one thing at a time), serving your application to thousands of people can be challenging!
  • Especially since R is used to do a lot of heavy lifting in connection to statistical modeling/machine learning
  • shiny has the ability to now help support these efforts using asynchronous operations

ASYNC + FUTURES

  • We're not diving into this, so don't worry!
  • I just wanted to let you know that shiny is capable and worth learning!

FOLLOW UP RESOURCES

THANK YOU


🙏

Questions?