Part III

Illya Mowerman

6/4/2018

Welcome

What we’ll cover

Some examples

https://shiny.rstudio.com/gallery/

Please install and load some pakages

install.packages('shiny')

install.packages('shinydashboard')

install.packages('tidyverse')

Components of a Shiny App

ui: what the user sees, and where they interact with the app

server: the engine behind the app

Starter code

ui <- fluidPage(

  # App title ----
  titlePanel(" "),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel( ),

    # Main panel for displaying outputs ----
    mainPanel( )
  )
)

server <- function(input, output) { }

shinyApp(ui, server)

Graphs: let’s create a graph using ggplot2

library(tidyverse)

hist_var <- 'mpg'

hist_title <- 'my title'

ggplot(mtcars) +
  geom_histogram(aes_string(hist_var)) +
  labs(title = hist_title)

The “thing” is to create paramerized code

Text Input

textInput(inputId, label, value = "", width = NULL, placeholder = NULL)

Tables: let’s create a table for viewing based on a parametrized filter

library(tidyverse)

min_mpg <- 20

max_mpg <- 25

mtcars %>% 
  filter(mpg >= min_mpg , mpg <= max_mpg)
##    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## 1 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## 2 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## 3 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## 4 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## 5 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## 6 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## 7 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## 8 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

Slider Input

sliderInput(inputId, label, min, max, value, step = NULL, round = FALSE,
  format = NULL, locale = NULL, ticks = TRUE, animate = FALSE,
  width = NULL, sep = ",", pre = NULL, post = NULL, timeFormat = NULL,
  timezone = NULL, dragRange = TRUE)

Upload button

UI

fileInput(inputId, label, multiple = FALSE, accept = NULL, width = NULL,
  buttonLabel = "Browse...", placeholder = "No file selected")

Server

server <- function(input, output) {
  output$contents <- renderTable({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    read.csv(inFile$datapath, header = input$header)
  })
}

Until next time