Shiny
Shiny is a web development framework in R. It allows you to create a graphical interface so that users can interact with visualisations, models and algorithms without needing to know R themselves.
An Application
When you create an app, there’s a ui.R and a server.R.
Within the UI, you start off with a shinyUI()
call which one arugment:
- A
fluidPage()
call to set up the page. This function has two arguments:- A
titlePanel()
to set the title. - A
sidebarLayout()
to set the sidebar, and - A
mainPanel()
to set the main content.
- A
Inputs and Outputs
Slider
A slider can be created as such on the UI side:
Then on the server side:
Then back on the UI we can use
to output the value.
Plots
Shiny provides the plotOutput()
function and the renderPlot()
for taking user input and creating plots
On the user side we use plotOutput('plot_name')
. On the server side we use the render plot function. Here’s an example:
Reactivity
A reactive expression manipulates inputs from Shiny and returns a value. It provides a way for an app to respond since inputs will change depending on how users interact with the user interface.
Expressions wrapped by reactive()
are subject to change.
You may not want the app to immediately react to changes in user input, as the calculation may be long running. This is where delayed reactivity comes in.
You add a button in the UI and this delays the reactivity:
Tabs
There are several other kinds of UI components including tabs, navbars, and sidebars.
You can use tabs:
Interactive Graphics
You can create graphics that the user an interact with. One method is to select multiple data points on a graph by using the brush
argument in plotOutput()
on the server side.
On the UI side:
On the server side:
Shiny Gadget
Shiny gadget is a function that launches a small, single page Shiny application.
library(shiny)
library(miniUI)
a_gadget <- function() {
ui <- miniPage(
gadgetTitleBar("A Gadget")
)
server <- function(i, o, session) {
observeEvent(i$done, {
stopApp()
})
}
rungadget(ui, server)
}
One of the advantages of gadgets is that they are functions, so they can take arguments and can return values.
You can also do interactive graphics.
pickTrees <- function() {
ui <- miniPage(
gadgetTitleBar('Select Points by Dragging your Mouse'),
miniContentPanel(
plotOutput('plot', height = '100%', brush = 'brush')
)
)
server <- function(i, o, session) {
o$plot <- renderPlot({
plot(trees$Girth, trees$Volume, main = 'Trees!')
})
observeEvent(i$done, {
stopApp(brushedPoints(trees, i$brush, xvar = 'Girth', yvar = 'Volume'))
})
}
runGadget(ui, server)
}
GoogleVis
Can create interactive charts:
## Creating a generic function for 'toJSON' from package 'jsonlite' in package 'googleVis'
##
## Welcome to googleVis version 0.6.4
##
## Please read Google's Terms of Use
## before you start using the package:
## https://developers.google.com/terms/
##
## Note, the plot method of googleVis will by default use
## the standard browser to display its output.
##
## See the googleVis package vignettes for more details,
## or visit https://github.com/mages/googleVis.
##
## To suppress this message use:
## suppressPackageStartupMessages(library(googleVis))
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
R version 3.6.1 (2019-07-05) • Google Terms of Use • Documentation and Data Policy
There are a number of other charts:
- Motion charts:
gvisMotionChart()
- Interactive maps:
gvisGeoChart()
- Interactive tables:
gvisTable()
- Line charts:
gvisLineChart()
- Bar charts:
gvisColumnChart()
- Tree maps:
gvisTreeMap()
Combine multiple plots together:
Plotly
Plotly is a web applications for creating and sharing data visualisations.
##
## Attaching package: 'plotly'
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
## The following object is masked from 'package:ggplot2':
##
## last_plot
Can change the colour of the points, size of the points
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
Three dimensional scatterplot:
tibble(
x = 1:100,
y = rnorm(100, 20, 10),
z = rnorm(100, 20, 2),
c = as.factor(sample(1:4, 100, replace = TRUE))
) %>%
plot_ly(x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'markers', color = ~c)
Choropleth
##
## Attaching package: 'glue'
## The following object is masked from 'package:dplyr':
##
## collapse
## Parsed with column specification:
## cols(
## code = col_character(),
## state = col_character(),
## category = col_character(),
## `total exports` = col_double(),
## beef = col_double(),
## pork = col_double(),
## poultry = col_double(),
## dairy = col_double(),
## `fruits fresh` = col_double(),
## `fruits proc` = col_double(),
## `total fruits` = col_double(),
## `veggies fresh` = col_double(),
## `veggies proc` = col_double(),
## `total veggies` = col_double(),
## corn = col_double(),
## wheat = col_double(),
## cotton = col_double()
## )
map_options <- list(
scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = TRUE,
lakecolor = toRGB('white')
)
df %>%
mutate(hover = glue("State: {code}")) %>%
plot_ly(
type = 'choropleth',
locations = ~code,
locationmode = 'USA-states',
z = ~`total exports`,
colors = 'Blues',
text = ~hover
) %>%
layout(title = 'USA Map', geo = map_options)