class: center, middle, inverse, title-slide # Interactive Plotting ## with Shiny ### Basel R Bootcamp
www.therbootcamp.com
@therbootcamp
### July 2018 --- layout: true <div class="my-footer"><span> <a href="https://therbootcamp.github.io/"><font color="#7E7E7E">BaselRBootcamp, July 2018</font></a>                                         <a href="https://therbootcamp.github.io/"><font color="#7E7E7E">www.therbootcamp.com</font></a> </span></div> --- <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/matrix_shiny.png" width="100%" style="display: block; margin: auto;" /> --- ## This is Shiny <iframe src="https://shiny.rstudio.com/gallery/movie-explorer.html" width="1200" height="500"></iframe> --- # What is Shiny? <p align="center"> <img src="https://github.com/therbootcamp/therbootcamp.github.io/blob/master/_sessions/_image/shiny_definition.png?raw=true" height="450px"></img><br> <a href="http://shiny.rstudio.com/https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/shiny-cheatsheet.pdf">RStudio cheat sheet</a> </p> --- # What does that mean? .pull-left4[ <high>Create interactive websites</high> to explore, analyse, and visualize data all within R Studio! No need to learn `HTML`, `CSS`, `Javascript`! <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/nohtml.png" height="300px" vspace="20px"></img><br> </p> ] .pull-right5[ <u>Example</u> <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/shiny_ss.png"></img><br> </p> ] --- ## Scatterplot App <iframe src="https://econpsychbasel.shinyapps.io/FinalApp/" width="1200" height="500"></iframe> --- ## FFTrees Shiny App <iframe src="https://econpsychbasel.shinyapps.io/shinyfftrees/" width="1200" height="500"></iframe> <font size=3><a href="https://econpsychbasel.shinyapps.io/shinyfftrees/">econpsychbasel.shinyapps.io/shinyfftrees</a></font> --- ## Balloon Analogue Risk Task (BART) Shiny App <iframe src="https://econpsychbasel.shinyapps.io/shinypsych_bart/" width="1200" height="500"></iframe> <font size=3><a href="https://econpsychbasel.shinyapps.io/shinypsych_bart/">econpsychbasel.shinyapps.io/shinypsych_bart</a></font> --- # Shiny <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/whatyoucando.png" height="450px" bh></img><br> </p> --- .pull-left4[ # Shiny is a new world... Shiny is built on R, but it is a <high>very</high> different animal. <br> <u>Good News</u> All of your standard plotting and analysis code will still be <high>regular R</high> <br> <u>Bad News</u> To program a full app, you must learn the <high>reactive</high> language of Shiny. Learn by example. Tons of shiny apps online. Most code is available. ] .pull-right55[ <br> <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/learnshiny_B.png"></img><br> </p> <center><font = size=4><a href = http://Shiny.rstudio.com>shiny.rstudio.com</font>, </a><font = size=4><a href = "https://shiny.rstudio.com/gallery/">RStudio Shiny Example Gallery</font></a> ] --- # How programming a Shiny App looks <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/shinyprogramming_ss.png" height="450px"></img><br> </p> --- # Structure of a Shiny App <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/uiandserver.png" height="450px"></img><br> </p> --- <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> <p align = "center"> <font size=5>P.S. You'll create a very similar app in the practical!</font> </p> <div class="center_text_2"> <span> Let's explore the user interface of an app! </span> </div> --- # User Interface <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/exampleapp_A.png" height="500px"></img><br> </p> --- # User Interface <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/exampleapp_B.png" height="500px"></img><br> </p> --- # User Interface <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/exampleapp_C.png" height="500px"></img><br> </p> --- # User Interface <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/exampleapp_D.png" height="500px"></img><br> </p> --- # User Interface <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/exampleapp_E.png" height="500px"></img><br> </p> --- # User Interface <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/exampleapp_F.png" height="500px"></img><br> </p> --- # User Interface <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/exampleapp_G.png" height="500px"></img><br> </p> --- # User Interface <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/exampleapp_H.png" height="500px"></img><br> </p> --- # User Interface <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/exampleapp_B.png" height="500px"></img><br> </p> --- # The final app! <iframe src="https://econpsychbasel.shinyapps.io/histogram_app/" width="1200" height="500"></iframe> --- # User Interface Main components: <high>Widgets</high>, <high>layouts</high>, and <high>plots</high> <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/userinterface_description.png" height="400px"></img><br> </p> --- # User Interface, Widgets Widgets are simple fields added to the user interface for users to add inputs. <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/widgetcode_output.png" width="70%" style="display: block; margin: auto;" /> --- # Server .pull-left35[ All code that creates plots, does machine learning ... (really anything!) goes in the <high>server()</high> function. To present output like a plot, it must be <high>rendered</high> in the server using a special rendering function like `renderPlot()` Once it is rendered, it is sent to the output and displayed in the user interface using an <high>output function</high> like `plotOutput()`. ] .pull-right6[ <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/serverdescription.png" height="400px"></img><br> </p> ] --- .pull-left45[ # Server ```r library(shiny) # User Interface: ui <- fluidPage( mainPanel( textInput("Title", "Title"), plotOutput("myplot") # Create the plot output$displot ) ) server <- function(input, output) { x <- ChickWeight$weight # Define x # Send rendered plot to output output$myplot <- renderPlot({ hist(x, main = input$Title) }) } shinyApp(ui = ui, server = server) ``` ] .pull-right45[ <br> <iframe src="https://econpsychbasel.shinyapps.io/barebones_hist/" width="800" height="550"></iframe> ] --- # Publishing (hosting) an app .pull-left45[ You can always <high>run a Shiny app locally</high> on your machine. To get it online, you need to put it on a <high>Shiny Server</high>. Publish an app (with some restrictions) at http://shinyapps.io from RStudio with one click! Install a local server at your business (RStudio: $10,000 / year) Other providers exist (e.g.; Amazon Web Services) ] .pull-right45[ <p align="center"> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/shinypublish.png" height="400px"></img><br> </p> ] --- # Practical In the practical, we will create the following app from scratch, and publish it online! <iframe src="https://econpsychbasel.shinyapps.io/FinalApp/" width="1200" height="410"></iframe> --- # Practical <p> <font size=6> <a href="https://therbootcamp.github.io/BaselRBootcamp_2018July/_sessions/Shiny/Shiny_practical.html"><b>Link to practical<b></a> </font> </p>