Laura Biggins
02/12/2020
There are two parts to a Shiny application
library(shiny)
ui <- fluidPage(
titlePanel("Simple app"),
radioButtons(inputId = "letter_choice",
label = "select a letter",
choices = LETTERS[1:5]),
numericInput(inputId = "number_choice",
label = "type in a number",
value = 4),
textOutput(outputId = "message")
)
server <- function(input, output) {
output$message <- renderText({
paste0("You selected the letter ", input$letter_choice,
" and the number ", input$number_choice)
})
}
shinyApp(ui, server)
The input object is a special type of named list.
From within the server code, the values of the input object can be accessed in the same way as list values.
Output function in UI code pairs with a render function in the server code.
Within the render function are instructions telling Shiny what to show the user.
library(shiny)
ui <- fluidPage(
titlePanel("Simple app"),
radioButtons(inputId = "letter_choice",
label = "select a letter",
choices = LETTERS[1:5]),
numericInput(inputId = "number_choice",
label = "type in a number",
value = 4),
textOutput(outputId = "message")
)
server <- function(input, output, session) {
output$message <- renderText({
paste0("You selected the letter ", input$letter_choice,
" and the number ", input$number_choice)
})
}
shinyApp(ui, server)
Save file as app.R within a folder named as the application name.
Each of these folders contains an app.R file.
a. Open the app.R file for Exercise 1.1 in RStudio and run the application.
b. Complete the tasks set out in the comments at the top of the script. These include setting default text, adding a label and changing the step size on the slider.
Create a Shiny application from scratch that looks (and functions) like the one below.
To get started use the shortcut provided by RStudio - start typing shinyapp
to autofill a shiny code snippet.
Exercise 1 app on server here
ui <- fluidPage(
titlePanel("Exercise 1.1"),
sidebarLayout(
sidebarPanel(
textInput(inputId = "user_name",
label = "Enter name"),
numericInput(inputId = "number_chosen",
label = NULL, value = 15),
sliderInput(inputId = "slider",
label = "Choose another number",
min = 10, max = 100, value = 50),
),
mainPanel(
textOutput(outputId = "supplied_name"),
textOutput(outputId = "info"),
plotOutput(outputId = "density_plot", height = "300px", width = "400px")
)
)
)
For each exercise open the app.R file and follow the instructions set out in the comments at the top of the script.
Unlike other inputs, fileInput() returns a data frame with 4 columns
default max file size is 5MB
library(shiny)
library(readr)
options(shiny.maxRequestSize = 10 * 1024^2)
ui <- fluidPage(
titlePanel("Uploading files"),
fileInput(inputId = "upload",
label = "Upload...",),
tableOutput("files")
)
server <- function(input, output) {
output$files <- renderTable({
req(input$upload)
dataset <- read_csv(input$upload$datapath)
head(dataset)
})
}
shinyApp(ui = ui, server = server)
fileInput() returns a data frame of 4 columns
Use the accept parameter in fileInput() to limit files shown in browser.
Data can be imported, manipulated if necessary and saved as R data objects (.Rds or .RData). This can be done in a pre-processing script to allow the data to simply be loaded within the Shiny app.
Saving and loading one file
Saving a named list
Saving more than one dataset in .Rdata
Fixing bugs and making modifications
Open the app.R file for Exercise 4 and follow the instructions set out in the comments at the top of the script.
The app will not work initially as there are some bugs in the code.
The first task will be to debug the script to enable the app to work, before making some further modifications.
Uploading a file.
Create an app that looks like this one. It should:
- allow a csv file to be chosen and uploaded
- display the first few rows of data in a table
- have an option to change the number of rows displayed
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
# base R
iris[iris$Sepal.Length > 7 & iris$Petal.Width < 2, ]
# tidyverse
iris %>%
filter(Sepal.Length > 7 & Petal.Width < 2)
# data masking -> minimises repetition
base R
tidyverse
base R
tidyverse
✔
Shiny with base R
Shiny with tidyverse
UI code in a Shiny app consists of functions to create html.
<h2>Choosing letters</h2>
<div id="letter_choice" class="form-group shiny-input-radiogroup shiny-input-container">
<label class="control-label" for="letter_choice">select a letter</label>
<div class="shiny-options-group">
<div class="radio">
<label>
<input type="radio" name="letter_choice" value="A" checked="checked"/>
<span>A</span>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="letter_choice" value="B"/>
<span>B</span>
</label>
</div>
</div>
</div>
<div id="message" class="shiny-text-output"></div>
HTML elements usually consist of a start and end tag, with the content inserted between the two tags. Shiny provides helper functions for creating the most common HTML elements
Some of the helper functions such as the header tags are very simple.
h1(), h2(), h3(), h4(), h5(), h6()
are functions for turning a piece of text into a title or header, h1 being the top level heading, h2 a subheader, h3 the next level subheader etc.
h1()
is a wrapper function for generating the <h1>
html tag
produces the following html:
<h1>Simple app</h1>
The other header tags (h2 - h6) work in a similar way.
The package shinythemes provides a range of themes to change the appearance of your Shiny app.