Objective

Learn what the heck shiny is and how you use it!

What's Shiny?

Quick example

library(data.table)
library(shiny)
defaultdisplay<-list(
    width="100%", height="75%"
  )
shinyAppDir(
  system.file("examples/06_tabsets", package="shiny"),
  options = defaultdisplay
)

Complex example

Teaching stats with shiny

Complex example

Board pack <!--

Why shiny, not PowerBI?

  • Firstly, PowerBI is amazeballs
  • It's excellent at allowing easy consumption
  • It does not however facilitate a workflow
  • Shiny does the dashboarding stuff + processing -->

Shiny structure

Contents | Typical

A shiny application report consists of two functions[^1]

One says what to execute and the other states how to present it the results.

[^1]: Don't actually need to use these anymore

Contents | "Lite"

defaultdisplay<-list(width="100%", height="75%")

shinyApp(
    ui      = fluidPage()
  , server  = function(input, output) {}
  , options = defaultdisplay
)

Files

You typically split into two files:

This can then be run with runApp()

You can do a single file example app.R which contains both functions but this is typically better for very short apps.

Front-end layout

lsp <- function(package, all.names = FALSE, pattern,...) 
{
  package <- deparse(substitute(package))
  ls(
      pos = paste("package", package, sep = ":"), 
      all.names = all.names, 
      pattern = pattern
  )
}
pages<-lsp(shiny,pattern = "Page")
pages<-pages[-grep(pattern ="^update|Handler$",pages)]
knitr::kable(data.table(`Page Types`=pages,` `=""))

Some basic objects

List of (standard) input types

inputs<-lsp(shiny,pattern = "Input")
inputs<-inputs[-grep(pattern ="^update|Handler$",inputs)]
knitr::kable(data.table(`Input controls p1`=inputs[1:6],
           `Input controls p2`=c(inputs[7:11],"")
))

List of (standard) output types

outputs<-lsp(shiny,pattern = "Output")
knitr::kable(data.table(`Output controls p1`=outputs[1:4],
           `Output controls p2`=outputs[5:8]
))

Dates

shinyApp(
  ui = fluidPage(dateInput("datePicker"
                , "Pick a date:"
                , format="dd/mm/yy")),
  server = function(input, output) {}
  ,options = defaultdisplay
)

Values | Sliders

shinyApp(
  ui = fluidPage(sliderInput("vals"
              ,"Insert a number:"
              ,min=0 ,max=50 ,value=15
              ,ticks=FALSE))
  ,server = function(input, output) {}
  ,options = defaultdisplay
)

Text | Paragraph

shinyApp(
  ui = fluidPage(tags$textarea(id="charbox"
                    ,rows=3
                    ,cols=40
                    ,"Default value"))
  ,server = function(input, output) {}
  ,options = defaultdisplay
)

Selectors

shinyApp(
  ui = fluidPage(selectInput("multiselect"
              ,"Pick favourites:"
              ,c("Green","Red","Blue")
              ,multiple=TRUE))
  ,server = function(input, output) {}
  ,options = defaultdisplay
)

Text

shinyApp(
  ui = fluidPage(textInput("char", "Insert text:") ,
                 textOutput("text")  ),
  server = function(input, output) {
    output$text <- renderText(input$char)
  }  ,options = defaultdisplay
)

Let's make things short

quickShiny <- function(uFUN,sFUN,s) {
  require(shiny)
  defaultdisplay <- list(width = "100%", height = "75%")
  shinyApp(
    ui = fluidPage(uFUN("a")),
    server = function(input, output) {
      output$a <- sFUN(s)
    },options = defaultdisplay
  )
}

Interactive tables

quickShiny(uFUN = DT::dataTableOutput,
           sFUN = DT::renderDataTable,
           s    = head(iris,5))

Charts

quickShiny(uFUN = plotOutput,
           sFUN = renderPlot,
           s    = pairs(iris[,1:3]))

Some funky objects

Leaflet maps

library(leaflet)
quickShiny(uFUN = leafletOutput,
           sFUN = renderLeaflet,
           s    = leaflet() %>%  addTiles() )

Dygraphs

library(dygraphs)
quickShiny(uFUN = dygraphOutput,
           sFUN = renderDygraph,
           s    = dygraph(mdeaths))

Graphs

library(DiagrammeR)
quickShiny(uFUN = grVizOutput,
           sFUN = renderGrViz,
           s    = render_graph(create_random_graph(3,3)))

Plot.ly

library(plotly)
quickShiny(uFUN = plotlyOutput,
           sFUN = renderPlotly,
           s    = plot_ly(z = volcano, type = "surface"))

Reactivity

Simple

uilist<-list(textInput("a","Text","Txt")
      ,textOutput("b"))
shinyApp(ui = fluidPage(uilist)
  ,server = function(input, output) {
    output$b <-renderText(paste0(input$a,"er"))
  },options = defaultdisplay
)

DRY

shinyApp(ui = fluidPage(uilist)
  ,server = function(input, output) {
    rand<-reactive({gen<-rpois(1,5)+1
                   paste0(gen,input$a)})
    output$b <-renderText(rand())
  },options = defaultdisplay
)

Isolated

uilist[[3]]<-actionButton("go","Go")
shinyApp(ui = fluidPage(uilist)
  ,server = function(input, output) {
    rand<-eventReactive(input$go
                  ,{gen<-rpois(1,5)+1
                   paste0(gen,input$a)})
    output$b <-renderText(rand())
  },options = defaultdisplay
)

Styling

CSS

shinythemes

shinydashboards

Infrastructure

Ad-hoc shiny

Cloud

Central server

Wrap up

Conclusion

  • shiny is an easy framework
  • shiny is very extendable
  • shiny has a variety of deployment options

Get the code

if (!require(devtools)) install.packages("devtools")
devtools::install_github("stephlocke/Rtraining")

Q & A

Thank you

  • Attendees
  • Organisers and volunteers

Recommended reading


The end!



stephlocke/Rtraining documentation built on May 30, 2019, 3:36 p.m.