library(shiny)
ui <- fluidPage(
)
server <- function(input, output, session) {
}
shinyApp(ui, server)Building a shiny app
SISBID 2025
https://github.com/dicook/SISBID
Shiny Apps in R
shinypackage 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,
Filemenu,New file,Shiny web appto 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)
sidebar <- sidebarPanel(width = 3, "Fun inputs")
main_col <- column(width = 9, "Some results")
ui <- fluidPage(
title = "App Title",
sidebar,
main_col
)
server <- function(input, output, session) {
}
shinyApp(ui, server)A bit more fancy
library(shiny)
sidebar <- sidebarPanel(
width = 3,
textInput("name", "Enter your name:", value = "Susan")
)
main_col <- column(width = 9, "Some results")
ui <- fluidPage(
title = "App Title",
sidebar,
main_col
)
server <- function(input, output, session) {
}
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)
sidebar <- sidebarPanel(
width = 3,
textInput("name", "Enter your name:", value = "Susan")
)
main_col <- column(width = 9, "Some results")
ui <- fluidPage(
title = "App Title",
sidebar,
main_col
)
server <- function(input, output, session) {
}
shinyApp(ui, server)Adding Output
- Output needs to be specified in two places:
- in the
uifunction we need to create a placeholder to reserve space for the output - in the
serverfunction, we need to create the output.
- in the
uicomponents are usuallyxxxOutput (outputID)
Output can be in form of tables, plots, text, …servercomponents arerenderXXX, wherexxxis 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.
