--- title: "Shiny Web Apps" author: "Thomas Girke" date: last-modified sidebar: tutorials bibliography: bibtex.bib ---
```{r setup} #| echo: false #| message: false #| warning: false suppressPackageStartupMessages({ library(ggplot2) }) ``` ## What is Shiny? [Shiny](https://shiny.posit.co/r/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, [shinyapps.io](http://www.shinyapps.io/) service). Since 2023, Shiny is also available for Python — see [Shiny for Python](https://shiny.posit.co/py/) — though this tutorial focuses on the R version. The basic structure of a Shiny app is an `app.R` script containing the following components: **Step 1.** User interface ```{r fluidpage} #| eval: false ui <- fluidPage() ``` **Step 2.** Server function ```{r server} #| eval: false server <- function(input, output) {} ``` **Step 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/refs/heads/main/tutorials/shinyapps/scripts/app.R). ```{r runshinyapp1} #| eval: false library(shiny) dir.create("myappdir") download.file("https://raw.githubusercontent.com/tgirke/GEN242/refs/heads/main/tutorials/shinyapps/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.posit.co/r/articles/share/). ```{r deployshinyapp1} #| eval: false setwd("myappdir") library(rsconnect) deployApp() ``` ## Example Shiny app The following Shiny app is hosted on `shinyapps.io` (here Instructor account) and embedded into the markdown (or html) source of this page using the following iframe syntax: ```html ``` Another example from Nick Harding using Shiny App: [volcanoshiny](https://github.com/hardingnj/volcanoshiny) Additional examples + Volcano Plots: [VolcaNoseR](https://huygens.science.uva.nl/VolcaNoseR/) ([publication](https://www.nature.com/articles/s41598-020-76603-3)). + systemPipeShiny: [web demo](https://tgirke.shinyapps.io/systemPipeShiny/) and [Bioc package](https://www.bioconductor.org/packages/release/bioc/html/systemPipeShiny.html) + spatialHeatmap: [web demo](https://spatialheatmap.org/) ## Resources to Learn Shiny ### Built-in examples The `shiny` package ships with example apps that are a good starting point. To explore them: ```{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). ``` Individual examples can also be loaded directly — see [Lesson 1](https://shiny.posit.co/r/getstarted/shiny-basics/lesson1/) on the Posit Shiny site. ### Tutorials and books - Official Shiny [tutorial videos and articles](https://shiny.posit.co/r/getstarted/) - Shiny [official gallery and source code](https://shiny.posit.co/r/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/) ### Extension packages and tools - Catalog of 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 - [shinylive](https://posit-dev.github.io/r-shinylive/) — Run Shiny apps entirely in the browser via WebAssembly (no server required); also embeddable directly in Quarto documents via `{shinylive-r}` chunks ## Session Info ```{r sessionInfo} sessionInfo() ``` ## References