This is an introduction to shiny web applications with R. Please follow the exercise to familiarise yourself with the fundamentals. And then you can follow instructions to build an app with interactive plots related to the Covid data. Code chunks starting with shinyApp()
can be simply copy-pasted to the RStudio console and run. Generally, complete shiny code is saved as a text file, named for example, as app.R and then clicking Run app launches the app.
This is an example to show the layout of widgets on a webpage using shiny functions. fluidPage()
is used to define a responsive webpage. titlePanel()
is used to define the top bar. sidebarLayout()
is used to create a layout that includes a region on the left called side bar panel and a main panel on the right. The contents of these panels are further defined under sidebarPanel()
and mainPanel()
.
In the main panel, the use of tab panels are demonstrated. The function tabsetPanel()
is used to define a tab panel set and individual tabs are defined using tabPanel()
. fluidRow()
and column()
are used to structure elements within each tab. The width of each column is specified. Total width of columns must add up to 12.
library(shiny)
ui <- fluidPage(
titlePanel("Title Panel"),
sidebarLayout(
sidebarPanel(
helpText("Sidebar Panel")
),
mainPanel(tabsetPanel(
tabPanel("tab1",
fluidRow(
column(6,helpText("Col1")),
column(6,
helpText("Col2"),
fluidRow(
column(4,style="background-color:#b0c6fb",
helpText("Col1")
),
column(4,style="background-color:#ffa153",
helpText("Col2")
),
column(4,style="background-color:#b1f6c6",
helpText("Col3")
)
)
)
)
),
tabPanel("tab2",
inputPanel(helpText("Input Panel"))
),
tabPanel("tab3",
wellPanel(helpText("Well Panel"))
)
)
)
)
)
server <- function(input,output){}
shinyApp(ui=ui,server=server)
Input widgets are used to accept content interactively from the user. These widgets usually end in Input
like selectInput()
. Below are usage examples of several of shiny’s built-in widgets. Every widget has a variable name which is accessible through input$
in the server function. For example, the value of a variable named text-input
would be accessed through input$text-input
.
shinyApp(
ui=fluidPage(
fluidRow(
column(6,
fileInput("file-input","fileInput:"),
selectInput("select-input",label="selectInput",choices=c("A","B","C")),
numericInput("numeric-input",label="numericInput",value=5,min=1,max=10),
sliderInput("slider-input",label="sliderInput",value=5,min=1,max=10),
textInput("text-input",label="textInput"),
textAreaInput("text-area-input",label="textAreaInput"),
dateInput("date-input",label="dateInput"),
dateRangeInput("date-range-input",label="dateRangeInput"),
radioButtons("radio-button",label="radioButtons",choices=c("A","B","C"),inline=T),
checkboxInput("checkbox","checkboxInput",value=FALSE),
actionButton("action-button","Action"),
hr(),
submitButton()
)
)
),
server=function(input,output){},
options=list(height=900))
Similar to input widgets, output widgets are used to display information to the user on the webpage. These widgets usually end in Output
like textOutput()
. Every widget has a variable name accessible under output$
to which content is written in the server function. Render functions are used to write content to output widgets. For example renderText()
is used to write text data to textOutput()
widget.
shinyApp(
ui=fluidPage(fluidRow(column(6,
textInput("text_input",label="textInput",value="<h3 style='color:red'>Red text</h3>"),
hr(),
htmlOutput("html_output"),
textOutput("text_output"),
verbatimTextOutput("verbatim_text_output"),
tableOutput("table_output"),
plotOutput("plot_output",width="300px",height="300px")
))),
server=function(input, output) {
output$html_output <- renderText({input$text_input})
output$text_output <- renderText({input$text_input})
output$verbatim_text_output <- renderText({input$text_input})
output$table_output <- renderTable({iris[1:3,1:3]})
output$plot_output <- renderPlot({
plot(iris[,1],iris[,2])
})
},
options=list(height=700))
In this example, we have a text input box which takes user text and outputs it in three different variations. The first output is html output htmlOutput()
. Since the default text is html content, the output is red coloured text. A normal non-html text would just look like normal text. The second output is normal text output textOutput()
. The third variation is verbatimTextOutput()
which displays text in monospaced code style. This example further shows table output and plot output.
Sometimes we want to add, remove or change currently loaded UI widgets conditionally based on dynamic changes in code execution or user input. Conditional UI can be defined using conditionalPanel()
, uiOutput()
/renderUI()
, insertUI()
or removeUI
. In this example, we will use uiOutput()
/renderUI()
.
In the example below, the output plot is displayed only if the selected dataset is iris.
shinyApp(
ui=fluidPage(
selectInput("data_input",label="Select data",
choices=c("mtcars","faithful","iris")),
tableOutput("table_output"),
uiOutput("ui")
),
server=function(input,output) {
getdata <- reactive({ get(input$data_input, 'package:datasets') })
output$ui <- renderUI({
if(input$data_input=="iris") plotOutput("plot_output",width="400px")
})
output$plot_output <- renderPlot({hist(getdata()[, 1])})
output$table_output <- renderTable({head(getdata())})
})
Here, conditional UI is used to selectively display an output widget (plot). Similarly, this idea can be used to selectively display any input or output widget.
sessionInfo()
## R version 4.1.3 (2022-03-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so
##
## locale:
## [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
## [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
## [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
## [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] grid stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] shiny_1.8.0 treeio_1.27.0.002 ggtree_3.11.0
## [4] pheatmap_1.0.12 swemaps_1.0 mapdata_2.3.1
## [7] maps_3.4.2 gridExtra_2.3 jpeg_0.1-10
## [10] ggpubr_0.6.0 cowplot_1.1.3 ggthemes_5.0.0
## [13] scales_1.3.0 ggrepel_0.9.5 wesanderson_0.3.7
## [16] forcats_1.0.0 stringr_1.5.1 purrr_1.0.2
## [19] readr_2.1.5 tidyr_1.3.1 tibble_3.2.1
## [22] tidyverse_2.0.0 reshape2_1.4.4 ggplot2_3.4.4
## [25] formattable_0.2.1 kableExtra_1.4.0 dplyr_1.1.4
## [28] lubridate_1.9.3 leaflet_2.2.1 yaml_2.3.8
## [31] fontawesome_0.5.2.9000 captioner_2.2.3 bookdown_0.37
## [34] knitr_1.45
##
## loaded via a namespace (and not attached):
## [1] nlme_3.1-155 fs_1.6.3 RColorBrewer_1.1-3
## [4] tools_4.1.3 backports_1.4.1 bslib_0.6.1
## [7] utf8_1.2.4 R6_2.5.1 lazyeval_0.2.2
## [10] mgcv_1.8-39 colorspace_2.1-0 withr_3.0.0
## [13] tidyselect_1.2.0 compiler_4.1.3 cli_3.6.2
## [16] xml2_1.3.6 labeling_0.4.3 sass_0.4.8
## [19] yulab.utils_0.1.4 systemfonts_1.0.5 digest_0.6.34
## [22] rmarkdown_2.25 svglite_2.1.3 pkgconfig_2.0.3
## [25] htmltools_0.5.7 fastmap_1.1.1 highr_0.10
## [28] htmlwidgets_1.6.4 rlang_1.1.3 rstudioapi_0.15.0
## [31] gridGraphics_0.5-1 jquerylib_0.1.4 farver_2.1.1
## [34] generics_0.1.3 jsonlite_1.8.8 crosstalk_1.2.1
## [37] car_3.1-2 magrittr_2.0.3 ggplotify_0.1.2
## [40] patchwork_1.2.0 Matrix_1.6-5 Rcpp_1.0.12
## [43] munsell_0.5.0 fansi_1.0.6 ape_5.7-1
## [46] abind_1.4-5 lifecycle_1.0.4 stringi_1.8.3
## [49] carData_3.0-5 plyr_1.8.9 promises_1.2.1
## [52] parallel_4.1.3 lattice_0.20-45 splines_4.1.3
## [55] hms_1.1.3 pillar_1.9.0 ggsignif_0.6.4
## [58] glue_1.7.0 evaluate_0.23 ggfun_0.1.4
## [61] leaflet.providers_2.0.0 httpuv_1.6.14 vctrs_0.6.5
## [64] tzdb_0.4.0 gtable_0.3.4 cachem_1.0.8
## [67] xfun_0.41 mime_0.12 xtable_1.8-4
## [70] broom_1.0.5 tidytree_0.4.6 later_1.3.2
## [73] rstatix_0.7.2 viridisLite_0.4.2 aplot_0.2.2
## [76] memoise_2.0.1 timechange_0.3.0 ellipsis_0.3.2