class: center, middle, inverse, title-slide # Interactive Webapps 💻 with Shiny ✨ ### Florencia D’Andrea (🐦cantoflor_87) ### Meetup R-Ladies Rcia-Ctes + R en el NEA ### 21/02/2020 --- class: center, middle # Licence <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>. --- class: center, inverse, bottom background-image: url("Shiny_grande.jpg") background-position: 50% 50% ## Interactive Webapps in R --- # Basics A Shiny app is a web page (UI) connected to a computer running a live R session (Server) <img src="cheat1.png" width="50%" style="display: block; margin: auto;" /> -- Users can manipulate the UI, which will cause the server to update the UI’s displays (by running R code). .footnote[Source: Shiny RStudio Cheatsheet.] --- # App.R 📄 ```r library(shiny) # User interfase UI <- fluidPage( ) # Server Server <- function(input, output){ } # Combines UI and server shinyApp (ui = ui, server = server) ``` You can insert all the code at once with the shinyapp snippet <img src="snippet_2.png" width="25%" style="display: block; margin: auto;" /> --- # Building a shiny app <img src="cheat1.png" width="50%" style="display: block; margin: auto;" /> .pull-left[ **UI** 1. Add the template -> ***app.R*** 1. Select a layout function 1. Select Inputs and place them ] .pull-right[ **Server** 1. Select `render*()` and `*Output()` functions 1. Refer to outputs with `output$<id>` 1. Refer to inputs with `input$<id>` ] --- # Run the app 🎬 * Click the Run App button in the document toolbar. <img src="run_app.png" width="25%" style="display: block; margin: auto;" /> * Use a keyboard shortcut: Cmd/Ctrl + Shift + Enter. * `shiny::runApp()` with the path to the directory containing **app.R** .footnote[Source: https://mastering-shiny.org/basic-app.html] --- # Cheatsheet <img src="cheatsheet.png" width="80%" style="display: block; margin: auto;" /> .footnote[Source: https://shiny.rstudio.com/articles/cheatsheet.html] --- class: center, middle # User Interfase (UI) <img src="mamu_4.jpg" width="90%" style="display: block; margin: auto;" /> --- # Layout Organize panels and elements into a layout with a **layout function** <img src="layout.png" width="90%" style="display: block; margin: auto;" /> .footnote[Source: Shiny RStudio Cheatsheet.] --- class: center, middle <img src="slide_mamu1.png" width="100%" style="display: block; margin: auto;" /> `Ui <- fluidPage(sidebarLayout(sidebarPanel(…), mainPanel(…))` --- # Inputs Collect values from the user <img src="inputs.png" width="100%" style="display: block; margin: auto;" /> .footnote[Source: Shiny RStudio Cheatsheet.] --- class: center, middle <img src="slide_mamu2.png" width="90%" style="display: block; margin: auto;" /> `Ui <- fluidPage(sidebarLayout(sidebarPanel(radioButtons(…)), mainPanel(…))` --- # Input selection `radioButtons()` arguments ```r radioButtons(inputId = `"axis_x"`, label = `"X"`, choices = c("Highway fuel economy (mpg)" = "hwy", "Engine displacement (L)" = "displ", "Car type" = "class")) ``` --- class: center, middle <img src="parentesis.png" width="90%" style="display: block; margin: auto;" /> --- class: center, middle # ⚠️ Warning! Families can be complex <img src="complex.jpg" width="70%" style="display: block; margin: auto;" /> --- class: center, middle <img src="closing_par.png" width="90%" style="display: block; margin: auto;" /> --- class: center, middle <img src="mamu_8_sd.jpg" width="90%" style="display: block; margin: auto;" /> --- class: inverse # Live coding #1 🙌💻 * Add the Shinyapp template * Layouts and inputs * Run the App ➡️📁 Rladies_app --- class: center, middle # Reactivity <img src="mamu_2.jpg" width="100%" style="display: block; margin: auto;" /> --- <img src="server_react.png" width="40%" style="display: block; margin: auto;" /> .pull-left[ **UI** 1. Add the template -> ***app.R*** 1. Select a layout function 1. Select Inputs and place them ] .pull-right[ **Reactivity** 1. Select `render*()` and `*Output()` functions 1. Refer to outputs with `output$<id>` 1. Refer to inputs with `input$<id>` ] --- # `render*()` and `*Output()` functions <img src="outputs.png" width="90%" style="display: block; margin: auto;" /> --- # Plot ```r library(ggplot2) ggplot(data = mpg)+ geom_point(mapping = aes(x = displ, y = hwy, color = class)) ``` <img src="rladies_shiny_2020_files/figure-html/plot1-1.png" style="display: block; margin: auto;" /> --- # Refer to inputs with `input$<id>` <img src="reactivity.png" width="90%" style="display: block; margin: auto;" /> --- # Refer to inputs with `input$<id>` <img src="reactivity3.png" width="90%" style="display: block; margin: auto;" /> --- # Refer to outputs with `output$<id>` <img src="reactivity.png" width="90%" style="display: block; margin: auto;" /> --- # Refer to outputs with `output$<id>` Observe `render*()` and `*Output()` functions! <img src="reactivity_plot.png" width="90%" style="display: block; margin: auto;" /> --- <img src="slide_mamu3.png" width="90%" style="display: block; margin: auto;" /> --- class: center, middle # Extras <img src="mamu_1.jpg" width="70%" style="display: block; margin: auto;" /> --- # Add a Theme <img src="shinythemes.png" width="70%" style="display: block; margin: auto;" /> --- # Add a Title ```r *ui<- fluidPage(theme = shinytheme("lumen"), * h1(strong("R-Ladies Buenos Aires")), * h4(em("Shiny meetup")), sidebarLayout( sidebarPanel( radioButtons(…) ), mainPanel(… )) ``` <img src="header.png" width="609" style="display: block; margin: auto;" /> .footnote[Source: Shiny RStudio Cheatsheet.] --- # Include an image 1. Place the file in the ***www*** subdirectory 1. Link to it with `img(src="<file name>")` <img src="structure.png" width="90%" style="display: block; margin: auto;" /> --- class: inverse # Live coding #2 🙌💻 * Server * Reactivity * Run the app ➡️📁 Rladies_app --- class: inverse, center, middle <img src="appfinal.png" width="90%" style="display: block; margin: auto;" /> --- class: inverse, center, middle # Repo https://github.com/flor14/rladies_shiny_meetup_2020 ![](https://media.giphy.com/media/X8HbeXDF7nzaM/200w_d.gif) --- class: inverse, center, middle # Examples [RStudio Shiny Gallery](https://shiny.rstudio.com/gallery/) [Winners of the 1st Shiny Contest](https://blog.rstudio.com/2019/04/05/first-shiny-contest-winners/) --- class: inverse, center, middle # Links [Mastering Shiny](https://mastering-shiny.org/) [Shiny's Holy Grail - UseR! 2019 - Joe Cheng video](https://www.youtube.com/watch?v=5KByRC6eqC8) [Tutorial R-Studio](https://shiny.rstudio.com/tutorial) [Javascript for Shiny Users](https://github.com/rstudio-conf-2020/js-for-shiny) [Golem Package](https://github.com/ThinkR-open/golem) [Shinytest](https://rstudio.github.io/shinytest/articles/package.html) --- class: center, middle # Thanks! ![](https://media.giphy.com/media/3oEjHWPTo7c0ajPwty/giphy.gif) Slides created via the R package [**xaringan**](https://github.com/yihui/xaringan). The Template comes from [Allison Horst](https://remarkjs.com)