library(shiny)
<- fluidPage(
ui
)
<- function(input, output, session) {
server
}
shinyApp(ui, server)
Building a shiny app
SISBID 2025
https://github.com/dicook/SISBID
Shiny Apps in R
shiny
package developed and supported by RStudio (Winston Chang, Joe Cheng, JJ Allaire and others)- Resources for shiny: https://shiny.rstudio.com/:
Next five minutes
- Create a new shiny app in RStudio
- Run it
- Stop it
Create your first app
- In RStudio,
File
menu,New file
,Shiny web app
to start a new app - The easiest start is the
Single file
, which will put both server and ui functions in the same file,app.R
Two main parts
- What we see and interact with:
- user interface: layout with user input and (plot) output
- What is going on underneath:
- the server: glue between user input and output
A Minimal Example
A bit more fancy
library(shiny)
<- sidebarPanel(width = 3, "Fun inputs")
sidebar
<- column(width = 9, "Some results")
main_col
<- fluidPage(
ui title = "App Title",
sidebar,
main_col
)
<- function(input, output, session) {
server
}
shinyApp(ui, server)
A bit more fancy
library(shiny)
<- sidebarPanel(
sidebar width = 3,
textInput("name", "Enter your name:", value = "Susan")
)
<- column(width = 9, "Some results")
main_col
<- fluidPage(
ui title = "App Title",
sidebar,
main_col
)
<- function(input, output, session) {
server
}
shinyApp(ui, server)
Shiny Inputs
Shiny has many different input options, see the widget gallery:
actionButton()
- creates a clickable buttonselectInput()
create a select listcheckboxInput()
andcheckboxGroupInput()
dateInput()
- calendar to select a datedateRangeInput()
- select a range of datesfileInput()
- upload a filenumericInput()
- input a numeric valueradioButtons()
- select one or more itemssliderInput()
- slide along a range of valuestextInput()
- input a string
Your Turn
Add a list of your favorite countries to the sidebar panel:
library(shiny)
<- sidebarPanel(
sidebar width = 3,
textInput("name", "Enter your name:", value = "Susan")
)
<- column(width = 9, "Some results")
main_col
<- fluidPage(
ui title = "App Title",
sidebar,
main_col
)
<- function(input, output, session) {
server
}
shinyApp(ui, server)
Adding Output
- Output needs to be specified in two places:
- in the
ui
function we need to create a placeholder to reserve space for the output - in the
server
function, we need to create the output.
- in the
ui
components are usuallyxxxOutput (outputID)
Output can be in form of tables, plots, text, …server
components arerenderXXX
, wherexxx
is a table, plot, text, etc.
Shiny Outputs
Shiny also has a variety of different output options:
renderDataTable()
- outputs an interactive, sortable data tablehtmlOutput()
- output html elementsrenderPlot()
- output an R plotrenderPrint()
- output text from print() in RrenderTable()
- output an HTML tablerenderText()
- output text from RrenderUI()
- output a custom part of the user interfacerenderImage()
- print an image to the page
Adding a plot
library(shiny)
library(ggplot2)
library(dplyr)
sidebar <- sidebarPanel(
width = 3,
selectInput(
"country", "Pick your favorite country:",
choices = c("Australia", "Brazil", "China")
)
)
main_col <- column(
width = 9,
plotOutput("scatter")
)
ui <- fluidPage(
title = "App Title", sidebar, main_col
)
server <- function(input, output, session) {
output$scatter <- renderPlot({
mtcars %>% ggplot(aes(x = disp, y = mpg)) +
geom_point() +
ggtitle(input$country)
})
}
shinyApp(ui, server)
Your turn
- Add an interactive plot of the TB data into your app.
- Change the select box, so that the user can choose any country. Think of
unique(tb$country)
for defining the choices
Deploy an app
- Sign up for an account on https://www.shinyapps.io/
- Authenticate your account
- You may need to do some setup in your session, e.g. install the library
rsconnect
Resources
- RStudio Tutorial
- Deploy your app for others to use
- Shiny Setup, Showcase, and Server
- Community discussion
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.