class: center, middle, inverse, title-slide .title[ #
] .author[ ### ] --- class: center, middle, animated, bounceInDown #### Theory lessons <br> | Marta Coronado Zamora | Jose F. Sánchez | |:-:|:-:| | <a href="mailto:Marta.coronado@uab.cat"><i class="fa fa-paper-plane fa-fw"></i> marta.coronado@uab.cat</a> | <a href="mailto:JoseFrancisco.Sanchez@uab.cat"><i class="fa fa-paper-plane fa-fw"></i> josefrancisco.sanchez@uab.cat</a> | | <a href="https://bsky.app/profile/geneticament.bsky.social"><i class="fab fa-bluesky fa-fw"></i> @geneticament</a> | <a href="https://twitter.com/JFSanchezBioinf"><i class="fab fa-twitter fa-fw"></i> @JFSanchezBioinf</a> | | <a href="https://portalrecerca.uab.cat/es/organisations/grup-de-gen%C3%B2mica-bioinform%C3%A0tica-i-biologia-evolutiva-gbbe/"><i class="fa fa-map-marker fa-fw"></i> Universitat Autònoma de Barcelona </a> | <a href="http://www.germanstrias.org/technology-services/genomica-bioinformatica/"> <i class="fa fa-map-marker fa-fw"></i>Germans Trias i Pujol Research Institute (IGTP)</a> | #### Practical lessons <br> | Miriam Merenciano | |:-:| | <a href="mailto:miriam.merenciano@uab.cat"><i class="fa fa-paper-plane fa-fw"></i> miriam.merenciano@uab.cat </a> | | <a href="https://portalrecerca.uab.cat/es/organisations/grup-de-gen%C3%B2mica-bioinform%C3%A0tica-i-biologia-evolutiva-gbbe/"><i class="fa fa-map-marker fa-fw"></i> Universitat Autònoma de Barcelona </a> | <style> .title-slide { background-image: url('img/1.png'); background-size: 100%; } </style> --- layout: true class: animated, fadeIn --- # Session content - Interactive data visualization - In R + `htmlwidgets` + `shiny` --- layout: false class: left, bottom, inverse, animated, bounceInDown # Get started! ## Interactive and dynamic data visualization --- layout: true class: animated, fadeIn --- ## Example 1 <img src="3_files/figure-html/unnamed-chunk-1-1.gif" alt="" style="display: block; margin: auto;" /> --- ## Example 2
--- ## Example 3 <center> <img src="img/crosstalk2022_10_10D9_39_7.gif" width=100%> </center> [Link](https://bwlewis.github.io/crosstool/) --- ## Example 4 <center> <img src="img/movies2022_10_10D9_43_14.png" width=60%> </center> [Link](https://shiny.rstudio.com/gallery/movie-explorer.html) --- ## Example 4 <center> <img src="img/shinyMovies2022_10_10D9_46_53.gif" width=80%> </center> [Link](https://shiny.rstudio.com/gallery/movie-explorer.html) --- #### <i class="fas fa-pencil-alt"></i> **Exercise** | Describe the previous visualizations. Are they dynamic? And interactive? If so, what types of interaction do you identify? <div style="background-color:#F0F0F0">  <i class="fas fa-comment-dots"></i> Answer:   </div> --- ## Types of interaction - **Interactive plot area** + Action: hover, click, brush + Response: zoom, identify, link, add/remove - **Interactive controls** (outside the plot) + Action: click, drag + Response: choose data set, variables, parameters and redraw --- ## How interaction works .pull-left[ - **Basic framework**: web browsers `html` + `css` + `JavaScript` - Options for interaction: + **Client-side** Static `html` page with `JavaScript` code Executed within the browser + **Server-side** Dynamic `html` page Executed in the host machine ] .pull-right[ ] --- ## In R In recent years there has been a shift from using static R graphics to using interactive JavaScript web components for data analysis and data visualization: we need **R Bindings to JavaScript libraries** -- - The `htmlwidgets` package Provides a framework for creating R bindings to `JavaScript` libraries. HTML Widgets can be: + Used at the R console for data analysis just like conventional R plots + Embedded within R Markdown documents + Incorporated into Shiny web applications + Saved as standalone web pages Executed in browser (client-side) Standalone `html` file -- - `Shiny` applications Framework for interactive web applications in `R` Executed in host machine (server-side) Needs `R` running --- layout: false class: left, bottom, inverse, animated, bounceInDown # `htmlwidgets` --- layout: true class: animated, fadeIn --- ## What are `htmlwidgets`? .pull-left[ Bridge between `R` and `JavaScript` libraries: | `R` package | `JavaScript` library | |-------------|----------------------| | plotly | plotly.js (D3.js) | | networkD3 | D3.js | | dygraphs | Dygraphs.js | leaflet | leaflet.js | | ... | ... | [Full list of `htmlwidgets`](http://www.htmlwidgets.org/) ] .pull-right[ <center> <img src="img/htmlwdigets.JPG" width=100%> ] --- ## Example: `plotly`
--- ## Example: `networkD3`
<center> --- ## Example: `dygraphs`
--- ## Example: `leaflet`
--- #### <i class="fas fa-pencil-alt"></i> **Exercise** | Tranform the following graphics made with `ggplot2` into `plotly` interactive versions. What kind of interaction does `plotly` add? ``` r p1 <- ggplot(iris, aes(Sepal.Length, Petal.Length, shape = Species, colour = Species)) + geom_point() p2 <- ggplot(iris, aes(Species, Petal.Length, fill = Species)) + geom_boxplot() ``` <div style="background-color:#F0F0F0">  <i class="fas fa-comment-dots"></i> Answer:   </div> --- layout: false class: left, bottom, inverse, animated, bounceInDown # `Shiny` --- layout: true class: animated, fadeIn --- ## What is Shiny? - Framework to create dynamic, reactive html pages without `html`, `css` or `JavaScript` + There isn't any static html output + Needs an R session running - Creates web applications + Interactive visualizations + Much more [Gallery of `Shiny` apps](https://shiny.posit.co/r/gallery/) --- ## Syntax ``` r library(shiny) # Web page ui <- fluidPage() # Running R session server <- function(input, output){} # Connection ui + server shinyApp(ui = ui, server = server) ``` -- Two basic files: `ui.R` and `server.R` (or have them both in `app.R`) - `ui` + html specifications (R functions) + input functions + output functions - `server` + instructions to build and rebuild R objects + refers to input and output in `ui` --- ### **Let's create a Shiny app!** First, install the `shiny` package: ``` r install.packages("shiny") library(shiny) ``` Create a new `Shiny Web App` in RStudio: <center> <img src="img/35238232-c0e49fca-ff61-11e7-8d54-881d96e3968e.png" width=60%> --- ### **Let's create a Shiny app!** First, install the `shiny` package: ``` r install.packages("shiny") library(shiny) ``` As an application name you can type `test_app` and create a Single File (`app.R`) and save it in a Directory: <center> <img src="img/35238280-df3b5536-ff61-11e7-940f-4cb0345728e4.png" width=55%> -- </center> **Run the app! <img src="img/35238295-e70a328c-ff61-11e7-8da3-8c62c896e25e.png" width=10% align="middle">** --- ## Parts of the UI UI is <i>de facto</i> an HTML file. - In building `myui` (or `ui.R` file) what we really do is to construct an HTML file with R functions. - By default, uses [bootstrap](http://getbootstrap.com/), the most popular HTML, CSS, and JS framework for developing responsive, mobile first projects on the web. Parts of the UI:<br> -- 1. HTML tags<br> -- 2. Layout<br> -- 3. Input (control widgets)<br> -- 4. Output --- ### UI: HTML tags - You can build UI by using HTML tags - Use `names(tags)` to see all available tags #### <i class="fas fa-pencil-alt"></i> **Exercise** | Modify your `app.R` adding the following code inside `fluidPage()` and run the app again. ``` r ui <- fluidPage( titlePanel(HTML('<h1>Old Faithful Geyser Data</h1> <hr><br> <p> I`m a paragraph showing how to write <strong>bold</strong> and <em>italics</em>.<br> This is a <code>block of code</code>.<br> I can also put <a href="http://www.google.com">a link to google</a>.<br> And I can also add images!<br> <center><img src="https://ih0.redbubble.net/image.543360195.2115/pp,550x550.jpg" width=10%></center></p> <h2>The app</h2>')), # Sidebar with a slider input for number of bins ``` Which HTML tags are you able to identify? What are they used for? <div style="background-color:#F0F0F0">  <i class="fas fa-comment-dots"></i> Answer:   </div> --- ### UI: Layout - **Panels** + Panel functions are used to put a group of elements together into a single ‘panel’. + There are several panel functions defined in shiny: `absolutePanel()`, `conditionalPanel()`, `fixedPanel()`, `headerPanel()`, `inputPanel()`, `mainPanel()`, `navlistPanel()`, `sidebarPanel()`, `tabPanel()`, `tabsetPanel()`, `titlePanel()`, `wellPanel()`, - **Layouts** + Layout functions are used to organize panels and elements into an existing layout. + There are several layout functions defined in shiny: <center> <img src="img/layout.png" width=50%> --- ### UI: Layout #### <i class="fas fa-pencil-alt"></i> **Exercise** | Which layouts and panels are used in the `app.R`? <div style="background-color:#F0F0F0">  <i class="fas fa-comment-dots"></i> Answer:   </div> --- ### UI: Inputs (control widgets) - Wigets are web elements that users can interact with. - The standard Shiny widgets are: <center> <img src="img/widgets.png" width=75%> [Widget gallery](https://shiny.posit.co/r/gallery/widgets/widget-gallery/) --- ### UI: Inputs (control widgets) - Increase counter: `actionButton`, `actionLink` - TRUE/FALSE: `checkboxInput` - Date: `dateInput`, `dateRangeInput` - File: `fileInput` - Number: `numericInput` - Text: `textInput`, `passwordInput` - Select elements: `radioButtons`, `selectInput`, `sliderInput` - Trigger reaction: `submitButton` -- #### <i class="fas fa-pencil-alt"></i> **Exercise** | Which widget(s) are used in the `app.R`? <div style="background-color:#F0F0F0">  <i class="fas fa-comment-dots"></i> Answer:   </div> --- ### UI: Output - The output will be updated automatically when an input widget changes: **reactivity** + when an user manipulates the app, Shiny reruns parts of `server.R` to create an updated output Types of outputs: - Plot: `plotOutput` - Image: `imageOutput` - Text: `textOutput` - Code: `verbatimTextOutput` - Table: `tableOutput` --- ### UI and server: Output <center> <img src="img/outputs.png" width=75%> --- ## Shiny app summary #### <i class="fas fa-pencil-alt"></i> **Exercise** | Create a shiny app named 'Cars' with the following requirements: 1. **Layout**: sidebar layout. 2. A **slider** in the sidebar panel with `inputId` "nrows" and `label` "Number of rows:", which controls how many rows of the data set `datasets::cars` to use in the following analysis. The minimum value is 1, maximum value is 50 and default value is 10. 3. In the main panel, create a **scatterplot** with `x` axis 'speed' and `y` axis 'dist' on the top and a table showing the data on the bottom, using `outputId` "`carsPlot`" and "`carsTable`" respectively. 4. Use HTML tags to format the UI: indicating an app title and your name. <i class="fas fa-key"></i> You can use a subset of your data in ggplot as `cars[1:input$nrows,]` <div style="background-color:#F0F0F0">  <i class="fas fa-comment-dots"></i> Answer:   </div> #### Upload your app to [Atenea](https://atenea.upc.edu/course/section.php?id=907341) --- ## Shiny app summary <center> <img src="img/aspect2023_10_5D16_42_22.png" width=60%> --- layout: false class: inverse, center, middle, animated, bounceInDown ### Upload your app to [Atenea](https://atenea.upc.edu/course/section.php?id=907341)