January, 2018

Day 1 - Shiny Shiny Basics

Day 1 - Agenda

  • Introduction
  • Shiny Dashboard
  • Reactivity
  • App design

Introduction

Now that we know how to build reports in R the next step is to communicate these results to other team members and or executives. For this you have a couple of options. Most of you will be familiar with a word document. The challenge with a word document is that each time you update a graph and/or table you have to copy this table into the word document and re-do the styling of it so that it looks similar to other graphs/tables.

  • Rmarkdown is a type of document that you can create in R that automatically updates the graphs and tables (and can then be converted to a word document). Actually all my slides are made for Rmarkdown.
  • If you want more user interaction like changing parameters, filtering the input data, changing the segmentation, then it is time to move to something a bit more interactive. This is where Shiny comes in. Shiny allows you to create web applications that allow you to get some input from a user and use that input to update graphs and tables. (At which point you could allow the user to download the Rmarkdown version of the graphs and tables)

Main components

shinydashboard is a package that could be used to create elegant dashboards with little effort.

library(shiny)
ui<-dashboardPage(
  header = dashboardHeader(title = "Demo"),  
  sidebar = dashboardSidebar(...),  
  body = dashboardBody(...)
)
server <- function(input,output,session){}
shinyApp(ui = ui, server = server)

Sidebar

Body

  body = dashboardBody(
    tabItems(
      tabItem(
        tabName = "tab_1",
        h1("This is tab 1")
      ),
      tabItem(
        tabName = "tab_21",
        h1("This is tab 2.1")
      ),
      tabItem(
        tabName = "tab_22",
        h1("This is tab 2.2")
      )
    )
  )

Example

Lets run Day 5 - Shinydashboard basics example

Reactivity

Reactive values work together with reactive functions. Call a reactive value from within the arguments of one of these functions to avoid the error Operation not allowed without an active reactive context.

Example

Lets run Day 5 - Reactivity basics example

App Design

  • App design is the most important part of the process. It will give you a clear idea of what the client's requirements are, what you are going to develop and something to work towards.

  • It is important to understand the data requirements and how you need to transform it into the required reports.

  • Start by writing functions that create the necessary reports. Follow the data science tool chain to get to the required reports making sure to see where there are any overlapping data requirements so not to run the same code more than once.

  • Plug these functions into you shiny app by mapping the function inputs to input elements and the output to output elements.