Cheat-sheet: https://github.com/rstudio/cheatsheets/raw/master/shiny.pdf
To create a new app:
then
and
There should now be an App at
library(shiny)
ui = fluidPage("Title") # Create the html-code for the dashboard app
server = function(input, output) {} # R-code which creates the output to be added to the dashboard app
shinyApp(ui, server) # Start the server
Creates a User Interface with rows and 12 units wide, that fills the browser window. Fluid pages scale their components to fill all available browser width.
fluidPage(
...,
title = "Window Title",
theme = NULL
)
Creates a fluid page layout. The main code to create the user interface is the list of (optional) commands indicated by ...
Has two important optional arguments: the browser window title and optionally an alternative theme stylesheet (normally a css file in a subfolder called www in the folder for the app).
Runnig the function
fluidPage()
without any arguments just return an empty web page.
The user defined function server takes at least two inputs, input and output. They are both lists that get passed between the user interface (the web page) and the server (R code to make plots, tables etc.)
In input we have the user input collected from the web page, stored in different “inputID”
In output we store different graphical elements produced by the renderXxxxx-functions. These are reproduced on the web page by the corresponding xxxxxOutput-functions.
shinyApp(ui, server) start a web server to produce a web page with a frontend and a backend.
The code below render a page with two inputs: one radio button with two options (linear and logarithmic) and one list with multiple choises from the first four letter of the Latin alphabet.
library(shiny)
## Warning: package 'shiny' was built under R version 4.1.1
ui = fluidPage(
# organise two panels side by side, smaller side panel on left, bigger main panel on right
sidebarLayout(
sidebarPanel( # Side panel which collects user input
radioButtons(
inputId = "scale",
label = "Scale:",
choices = c("linear","logarithmic"),
selected = "linear"
),
selectInput(
inputId = "group",
label = "Select group or groups",
choices = LETTERS[1:4],
multiple = TRUE
)
),
# Show a plot of the generated distribution
mainPanel( # Main panel which shows output
textOutput("myScale"),
textOutput("myGroup")
)
)
)
server = function(input, output) { # R-code which creates the output to be added to the dashboard app
output$myScale = renderText(
print(input$scale)
)
output$myGroup = renderText(
print(input$group)
)
}
shinyApp(ui, server) # Start the server
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
To create dynamic output we need to pass the selected inputs from the user interface to the server side, where the plots will happen. The input is passed on in the list input in the two elements with inputId scale and group respectively.
The function on the server side
server = function(input, output) { # R-code which creates the output to be added to the dashboard app
output$myScale = renderText(
print(input$scale)
)
output$myGroup = renderText(
print(input$group)
)
}
create two outputs. One line of text using the R command print(input$scale) which prints the element scale from the list input to the output. The second command use the R command print(input$group) to print the element group from the same list. This are “rendered” to there graphical form by the function renderText.
The first text output is put in the list output in the element myScale and the second text output in myGroup in the same list.
In the main panel
mainPanel( # Main panel which shows output
textOutput("myScale"),
textOutput("myGroup")
)
the functions textOutput places the output on the web page.
The two functions renderText and textOutput work in tandem for text output.
First we generate some example data
library(data.table)
## Warning: package 'data.table' was built under R version 4.1.1
nrows = 20 # generate a data table with 20 rows
set.seed(12) # set random seed to 12 (or whatever) so that the example can be reproduced
dt = data.table(Group = sample(LETTERS[1:4], nrows, replace = TRUE), # sample uniformly from A to D
xvalue = sample(2:20, nrows, replace = TRUE), # sample 20 numbers from 2 to 20 for our x values
yvalue = sample(4:8, nrows, replace = TRUE), # sample 20 numbers from 4 to 8 for our x values
zvalue = factor(sample(1:3, nrows, replace = TRUE))) # sample 20 from 1 to 3 for z values
# saved as a factor variable, i.e. a categorial variable
print(dt)
## Group xvalue yvalue zvalue
## 1: B 8 7 2
## 2: B 17 6 2
## 3: D 14 8 2
## 4: C 5 7 1
## 5: C 9 6 1
## 6: B 12 8 3
## 7: A 15 6 2
## 8: A 8 5 3
## 9: D 13 8 2
## 10: B 6 7 1
## 11: D 3 5 3
## 12: C 8 8 1
## 13: B 15 4 2
## 14: A 8 7 2
## 15: B 20 8 3
## 16: A 5 5 2
## 17: B 14 7 2
## 18: D 14 6 2
## 19: B 17 8 3
## 20: B 6 5 2
We want to be able to select from the group column. Using data table syntax we can for example show the rows where Group is either B or C
group = c("B","C")
dt[Group %in% group]
To plot
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.1
ggplot(data = dt[Group %in% group], aes(x = xvalue, y=yvalue, colour = zvalue )) +
geom_point()
If scale = "logarithmic" add a logaritmic scale on the y axis to the plot
scale = "logarithmic" # setting the variable scale to "logarithmic
p = ggplot(data = dt[Group %in% group], aes(x = xvalue, y=yvalue, colour = zvalue )) +
geom_point()
if (scale == 'logarithmic') {
p = p + scale_y_log10()
}
p
Create a function that take the group and scale as an input and produce an apropriate plot:
myplot = function(group, scale = "linear") {
p = ggplot(data = dt[Group %in% group], aes(x = xvalue, y=yvalue, colour = zvalue )) +
geom_point()
if (scale == 'logarithmic') {
p= p + scale_y_log10()
}
return(p)
}
Now
myplot(c("A","C","D"), "logarithmic")
library(shiny)
library(ggplot2)
library(data.table)
nrows = 20 # generate a data table with 20 rows
set.seed(12) # set random seed to 12 (or whatever) so that the example can be reproduced
dt = data.table(Group = sample(LETTERS[1:4], nrows, replace = TRUE), # sample uniformly from A to D
xvalue = sample(2:20, nrows, replace = TRUE), # sample 20 numbers from 2 to 20 for our x values
yvalue = sample(4:8, nrows, replace = TRUE), # sample 20 numbers from 4 to 8 for our x values
zvalue = factor(sample(1:3, nrows, replace = TRUE))) # sample 20 from 1 to 3 for z values
# saved as a factor variable, i.e. a categorial variable
myplot = function(group, scale = "linear") {
p = ggplot(data = dt[Group %in% group], aes(x = xvalue, y=yvalue, colour = zvalue )) +
geom_point()
if (scale == 'logarithmic') {
p= p + scale_y_log10()
}
return(p)
}
ui = fluidPage(
# organise two panels side by side, smaller side panel on left, bigger main panel on right
sidebarLayout(
sidebarPanel( # Side panel which collects user input
radioButtons(
inputId = "scale",
label = "Scale:",
choices = c("linear","logarithmic"),
selected = "linear"
),
selectInput(
inputId = "group",
label = "Select group or groups",
choices = LETTERS[1:4],
multiple = TRUE
)
),
# Show a plot of the generated distribution
mainPanel( # Main panel which shows output
textOutput("myGroup"),
plotOutput("myPlot")
)
)
)
server = function(input, output) { # R-code which creates the output to be added to the dashboard app
output$myGroup = renderText(
print(input$group)
)
output$myPlot = renderPlot(
myplot(input$group, input$scale)
)
}
shinyApp(ui, server) # Start the server