--- title: Shiny Web Apps author: "Thomas Girke" date: "Last update: `r format(Sys.time(), '%d %B, %Y')`" output: html_document: toc: true toc_float: collapsed: true smooth_scroll: true toc_depth: 3 fig_caption: yes code_folding: show number_sections: true fontsize: 14pt bibliography: bibtex.bib weight: 16 type: docs editor_options: chunk_output_type: console --- ```{r style, echo = FALSE, results = 'asis'} BiocStyle::markdown() options(width=100, max.print=1000) knitr::opts_chunk$set( eval=as.logical(Sys.getenv("KNITR_EVAL", "TRUE")), cache=as.logical(Sys.getenv("KNITR_CACHE", "TRUE"))) ``` ```{r setup, echo=FALSE, messages=FALSE, warnings=FALSE} suppressPackageStartupMessages({ library(ggplot2) library(fgsea) }) ```
Source code downloads:     [.Rmd](https://raw.githubusercontent.com/tgirke/GEN242//main/content/en/tutorials/shinyapps/shinyapps.Rmd)     [.R](https://raw.githubusercontent.com/tgirke/GEN242//main/content/en/tutorials/shinyapps/shinyapps.R)
## What is Shiny? [Shiny](https://shiny.rstudio.com/gallery/) is an R-based environment for building interactive web applications for data analysis and exploration [@noauthor_undated-mi; @shiny1]. Since most JavaScript code is autogenerated by the environment, basic R knowledge is sufficient for developing Shiny apps. They can be deployed on local computers or web servers including custom and cloud-based servers (e.g. AWS, GCP, [shinyapp.io](http://www.shinyapps.io/) service). The basic structure of a Shiny app is an `app.R` script containing the following components: 1. User interface ```{r fluidpage, eval=FALSE} ui <- fluidPage() ``` 2. Server function ```{r server, eval=FALSE} server <- function(input, output) {} ``` 3. Statement to run shiny app ```{r shinyapp, eval=FALSE} shinyApp(ui = ui, server = server) ``` Alternatively, the `ui` and `server` functions can be organized in two script files, a `ui.R` and a `server.R` script, respectively. ## Develop and test Shiny app locally Open R and set session to parent directory (here `myappdir`) containing shiny script `app.R`, and the run it with the `runApp()` function. A sample `app.R` script for testing can be downloaded from [here](https://raw.githubusercontent.com/tgirke/GEN242/main/static/custom/scripts/app.R). ```{r runshinyapp1, eval=FALSE} library(shiny) dir.create("myappdir") download.file("https://raw.githubusercontent.com/tgirke/GEN242/main/static/custom/scripts/app.R", "./myappdir/app.R") runApp("myappdir") # To show code in app, add argument: display.mode="showcase" ``` This will open the app in a web browser. ## Deploy on web server This can be done on local or cloud systems. An easy solution is to get an account on [shinyapps.io](http://www.shinyapps.io/) and then deploy Shiny apps there. For details, see [here](https://shiny.rstudio.com/deploy/). ```{r deployshinyapp1, eval=FALSE} setwd("myappdir") library(rsconnect) deployApp() ``` ## Example Shiny app The following Shiny app is hosted on `shinyapps.io` and embedded into the markdown (or html) source of this page using the following iframe syntax: ```{r embedshiny, eval=FALSE} ``` ## Learning Shiny The Shiny section on the Rstudio site contains excellent [tutorials](https://shiny.rstudio.com/tutorial/). In addition, users may want to explore the example apps included in the `shiny` package. This can be done by loading the individual examples (see [here](https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/)) or saving the code to a user writable directory like this: ```{r learnshiny, eval=FALSE} mydir <- system.file("examples", package="shiny") dir.create('my_shiny_test_dir') file.copy(mydir, "my_shiny_test_dir", recursive=TRUE) setwd("my_shiny_test_dir/examples") runApp("01_hello") # Runs first example app in directory dir() # Lists available Shiny examples (directories). ``` ## Resources to learn Shiny ### Tutorial and books - Long video [tutorials](https://shiny.rstudio.com/tutorial/). - Shiny [official Lessons](https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/). - Shiny [official gallery and source code](https://shiny.rstudio.com/gallery/) - Advanced Shiny book - [Mastering Shiny](https://mastering-shiny.org/index.html) - Advanced web application in R book - [Javascript for R](https://book.javascript-for-r.com/) - Shiny Tutorial by Le Zhang (UCR) - [Shiny Tutorial](https://girke.bioinformatics.ucr.edu/GEN242/tutorials/shinyappslezhang/shinyapps) ### Extension packages - Catalog of cool extension packages - [Awesome Shiny](https://github.com/nanxstats/awesome-shiny-extensions) - [shinyWidgets](https://github.com/dreamRs/shinyWidgets) - UI components - [systemPipeShiny](https://systempipe.org/sps/) - A framework for workflow management and data visualization. - [spsComps](https://systempipe.org/sps/dev/spscomps/) - UI components, animations, server components - [shinyjs](https://deanattali.com/shinyjs/) - server end JavaScript communications ## Session Info ```{r sessionInfo} sessionInfo() ``` ## References