- Co-organizer of this Meetup group
- Business Intelligence Analyst at Barracuda Networks
- R user for almost 3 years, Shiny user for ~2 years
June 3, 2015
shinyApp
, which takes arguments ui
and server
install.packages(shiny) library(shiny) shinyApp(ui=fluidPage(), server=function(input, output) {}) shinyApp(ui=fluidPage("Hello, World!"), server=function(input, output) {}) shinyApp(ui=fluidPage(h1("Hello, World!")), server=function(input, output) {})
fluidPage() fluidPage("Hello, World!") fluidPage(h1("Hello, World!"))
server <- function(input, output) {} server("foo", "bar") .Last.value
app.R
:# app.R ui <- fluidPage(h1("Hello, World!")) server <- function(input, output) {} shinyApp(ui=ui, server=server)
runApp()
runApp(launch.browser=TRUE)
# app.R ui <- fluidPage( h1("Hello, World!"), tableOutput("data") ) server <- function(input, output) { output$data <- renderTable( data.frame(letter = letters[1:10]) ) } shinyApp(ui=ui, server=server)
output$data
in server
, but use the string "data"
in ui
selectInput()
:# app.R ui <- fluidPage( h1("Hello, World!"), sliderInput("slider", "choose a length for the dataset:", 1, 26, 10), tableOutput("data") ) server <- function(input, output) { output$data <- renderTable( data.frame(letter = letters[1:10]) ) } shinyApp(ui=ui, server=server)
# app.R ui <- fluidPage( h1("Hello, World!"), sliderInput("slider", "choose a length for the dataset:", 1, 26, 10), tableOutput("data") ) server <- function(input, output) { output$data <- renderTable( data.frame(letter = letters[1:input$slider]) ) } shinyApp(ui=ui, server=server)
# app.R ui <- fluidPage( h1("Hello, World!"), sliderInput("slider", "choose a length for the dataset:", 1, 26, 10), tableOutput("data") ) server <- function(input, output) { output$data <- renderTable({ df <- data.frame(a = runif(10000000), b = rnorm(10000000)) rows <- sample(nrow(df), input$slider) df[rows, ] }) } shinyApp(ui=ui, server=server)
renderTable
because our expression is more than one linedf
before calling renderTable
:# app.R ui <- fluidPage( h1("Hello, World!"), sliderInput("slider", "choose a length for the dataset:", 1, 26, 10), tableOutput("data") ) server <- function(input, output) { df <- data.frame(a = runif(10000000), b = rnorm(10000000)) output$data <- renderTable({ rows <- sample(nrow(df), input$slider) df[rows, ] }) } shinyApp(ui=ui, server=server)
df
once, when we first connected to the apprenderTable
call runs when input$slider
changes; that's reactivity!ui.R
, and the backend in server.R
:# ui.R fluidPage( h1("Hello, World!"), sliderInput("slider", "choose a length for the dataset:", 1, 26, 10), tableOutput("data") )
# server.R function(input, output) { df <- data.frame(a = runif(10000000), b = rnorm(10000000)) output$data <- renderTable({ rows <- sample(nrow(df), input$slider) df[rows, ] }) }
file.remove("app.R")
) and run it the same way as beforefluidPage
/function to variables anymoredf
is created when the function in server.R
runs, which is when a client (browser) connectsserver.R
:# server.R df <- data.frame(a = runif(10000000), b = rnorm(10000000)) function(input, output) { output$data <- renderTable({ rows <- sample(nrow(df), input$slider) df[rows, ] }) }
df
is generated once when we call runApp
, instead of each time someone connectsglobal.R
:# global.R df <- data.frame(a = runif(10000000), b = rnorm(10000000))
server.R
server.R
and global.R
as possiblefunctions.R
:# functions.R makeData <- function() { data.frame(a = runif(10000000), b = rnorm(10000000)) } makeTable <- function(df, n) { rows <- sample(nrow(df), n) df[rows, ] }
functions.R
in global.R
, and replace the old code to make df
:# global.R source("functions.R") df <- makeDate()
server.R
:function(input, output) { output$data <- renderTable(makeTable(df, input$slider)) }
renderTable
call in makeTable
? try it outhost
arg to runApp
:runApp(launch.browser=TRUE, host="0.0.0.0")
127.0.0.1
shinyapps
package to deploy right from the R consolecolumn
/fluidRow
, pageWithSidebar
)selectInput
, checkboxInput
, checkboxGroupInput
, radioButtons
)rCharts
& leaflet
(D3.js
visualizations)datatables
(interactive tabular output)shinydashboard
(ui tools for making attractive dashboards)Pros:
Cons: