## Introduction to Shiny <p class="cat_block"> <span class="image_note"> March 28, 2019 <br/>Tami L Crawford <br/>tami.crawford@gmail.com </span> </p> --- ## Overview <div class="chalkboard"> <ul> <li>Basics <li>Application/Input/Outputs/Layouts <li>rstudio.cloud <li>Demo/Example Apps (Birding, Gene Explorer, HTML Template) <li>Reactivity <li>Debugging <li>Modules </ul> </div>  --- # Basics - Open Source Web framework for building reactive web applications in R  --- - Install and Load Shiny ``` install.packages("shiny") library(shiny) ``` - Single File (app.R) - Multiple Files (ui.R, server.R)(optional global.R, images, css, helper scripts, etc) - <span style="font-size: 12pt">in RStudio, can use <span style="color: gray">New/New File/Shiny Web app</span> </span>  --- ### Application  --- ## Inputs  --- ## Input/Outputs  --- ## Layouts  --- ### Running Shiny App ``` shiny::runApp() shiny::runApp("path/to/app.R") ``` - in RStudo, use RunApp Control --- ## rstudio.cloud - Free to create account https://rstudio.cloud - Runs RStudio in browser - Create projects from scratch, from github, from an rstudio.cloud project --- # Demo - Birding App  - <a href="https://sharleenw.shinyapps.io/hamilton_cbc_shiny/" target="_blank">Birding App</a> <span style="font-size: 9pt; color: gray;">less than 150 lines of code</span> | [Blog Post](https://sharleenw.rbind.io/post/hamilton_cbc_part_3/hamilton-christmas-bird-count-part-3/) | [RStudio.cloud](https://rstudio.cloud/project/284222) https://rstudio.cloud/project/284222 --- # Demo - Interactive Gene Explorer  - <a href="https://ginberg.shinyapps.io/gene_explorer/" target="_blank">Interactive Gene Explorer</a> <span style="font-size: 9pt; color: gray;">~ 500 lines of code</span> Links: <span style="font-size: 9pt; color: gray;"><a href="https://github.com/ginberg/gene_explorer" target="_blank">github</a> | <a href="https://canvasxpress.org/html/index.html" target="_blank">CanvasXpress</a> | <a href="https://blog.dominodatalab.com/large-visualizations-canvasxpress/">Large visualisations incanvasXpress</a> | [rstudio.cloud](https://rstudio.cloud/project/284254) https://rstudio.cloud/project/284254</span> --- # Demo - HTML Template  - <a href="https://vivekkatial.shinyapps.io/uber_shiny/" target="_blank">Uber Explore - Shiny HTML Template Example</a> <span style="font-size: 9pt; color: gray;"><a href="https://rstudio.cloud/project/258358" target="_blank">RStudio Cloud</a> </span> # Reactivity  --- # Debugging - Reload app when source changes ``` options(shiny.autoreload = TRUE) ``` Must manually restart when there's a compilation error or when changes are in a module or code that is loaded via 'source' --- # Reactivity Log - Reactivity Log ``` devtools::install_github("rstudio/reactlog") library(shiny) library(reactlog) # tell shiny to log reactivity options("shiny.reactlog" = TRUE) # run a shiny app app <- system.file("examples/01_hello", package = "shiny") runApp(app) # once app has closed, display reactlog show_reactlog() ``` --- # Modules - the problem  See https://bit.ly/modules2019 (Effective Use of Shiny modules) --- ## Modules - The Solution  --- ### Exploratory Regression Shiny App (ERSA) ``` library(ERSA) library(mlbench) data(BostonHousing) f <- lm(crim ~ . , data = BostonHousing) exploreReg(f, BostonHousing) ``` <span style="font-size: 9pt; color: gray;">[rstudio.cloud](https://rstudio.cloud/project/284586) https://rstudio.cloud/project/284586</span>  --- # Sharing your app - shinyapps.io - runGitHub() - rstudio.cloud --- ## Links - [Shiny Cheatsheet](https://shiny.rstudio.com/articles/cheatsheet.html) - [Online Shiny Tutorial](https://www.rstudio.com/online-learning/#shiny_) - Birding App [blog post](https://sharleenw.rbind.io/post/hamilton_cbc_part_3/hamilton-christmas-bird-count-part-3/) - ExploreModels [github](https://github.com/WinVector/ExploreModels) - Interactive Gen Explorer [git](https://github.com/ginberg/gene_explorer) | [rstudio.cloud](https://rstudio.cloud/project/243079) | [shinyapps](https://ginberg.shinyapps.io/gene_explorer/) - [ERSA Demo (rstudio.cloud)](https://rstudio.cloud/project/284586) https://rstudio.cloud/project/284586 - [Shiny in Produciton Talk RStudio Conf 2019](https://resources.rstudio.com/rstudio-conf-2019/shiny-in-production-principles-practices-and-tools-joe-cheng) which describes CranWhales [github](https://github.com/rstudio/cranwhales.git) - [Effective Use of Shiny modules - RStudio Conf](https://resources.rstudio.com/rstudio-conf-2019/effective-use-of-shiny-modules-in-application-development) - [Reactlog 2.0: Debugging the state of Shiny - RStudio Confg 2019](https://resources.rstudio.com/rstudio-conf-2019/reactlog-2-0-debugging-the-state-of-shiny) --- 