Vaidotas Zemlys-Balevičius
2016-04-19
Shiny http://github.com/rstudio/shiny is an R package which lets you to create an web application. The main benefits:
For R users low entry barrier
Majority of statistical dashboards actually require little functionality. Shiny lets to do a professional looking dashboard with minimal costs.
Offers a bridge to complicated Javascript visualisations via hmtlwidgets
An example can be created using Rstudio: File>New File>Shiny Web App ...
Has two parts:
The server is a function with arguments input, output and session
The main shiny code which provides the actual logic of web app is the code of this anonymous function
All the input elements of UI are elements of input. They can be accessed with $: input$mySelect.
All the output elements of UI are elements of output.
The main goal is to connect inputs to outputs.
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
Let the user choose a specific visualisation property or what to visualise
The most important concept in Shiny apps
Behind your back, Shiny builds a dependence tree on what UI element depends on what element.
If one element is changed, all its descendents are changed too. This is how you get interactivity
HTML can be faster than you, especially if you do some intense calculations in the app.
Shiny provides tools for finer control: observeEvent, req.
observeEvent: only do something if specific event is triggered
req: wait for the correct value
It still can be a struggle to get the desired result, but it was worse in previous versions of shiny.
val <- reactive({
if(input$sel == "1") return(data1)
if(input$sel == "2") return(data2)
data2
})
output$plot <- renderPlot({
data = val()
plot(x=data$x, y=data$y)
})
testthat.