class: title-slide, left, bottom # Introduction to R and Rstudio ---- ## **Session 1** ### ### --- class: center, middle # Agenda Using R Studio Importing data --------- Break --------- Introduction to ggplot2 What does this function do? --------- Lunch ----------- Data wrangling with dplyr Naming objects | Relational data --------- Break --------- R Markdown Ongoing learning .green[Finish about 4 - 430pm] --- class: center, middle # Course Aims ####1. To show you some of the possibilities: ####2. To give you a feel for how R works. ####3. To show you enough for you to begin teaching yourself .blue[(Excellent free resources available)] --- class: inverse, middle, center .left-col[.center[ ### elegant # Graphics ]] --- [<img class="center" src="data:image/png;base64,#img/session01/heatmap_colin_angus.PNG"/>](https://github.com/VictimOfMaths/COVID-19) --- The original blog is no longer available that featured this <img class="center" src="data:image/png;base64,#img/session01/london_cycle_routes.PNG" width="90%"/> --- class: center, middle # Collaboration <svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M413.1 222.5l22.2 22.2c9.4 9.4 9.4 24.6 0 33.9L241 473c-9.4 9.4-24.6 9.4-33.9 0L12.7 278.6c-9.4-9.4-9.4-24.6 0-33.9l22.2-22.2c9.5-9.5 25-9.3 34.3.4L184 343.4V56c0-13.3 10.7-24 24-24h32c13.3 0 24 10.7 24 24v287.4l114.8-120.5c9.3-9.8 24.8-10 34.3-.4z"></path></svg> ## Reproducibility <svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M413.1 222.5l22.2 22.2c9.4 9.4 9.4 24.6 0 33.9L241 473c-9.4 9.4-24.6 9.4-33.9 0L12.7 278.6c-9.4-9.4-9.4-24.6 0-33.9l22.2-22.2c9.5-9.5 25-9.3 34.3.4L184 343.4V56c0-13.3 10.7-24 24-24h32c13.3 0 24 10.7 24 24v287.4l114.8-120.5c9.3-9.8 24.8-10 34.3-.4z"></path></svg> ### R Markdown --- <img class="center" src="data:image/png;base64,#img/session01/automated_reports.PNG"/> --- # (Interactive) Dashboards [Mental Health Surge Modelling](https://strategyunit.shinyapps.io/MH_Surge_Modelling/) <img src="data:image/png;base64,#img/session01/mental_health_modelling.PNG"/> --- [Trafford Data Lab](https://trafforddatalab.shinyapps.io/trafford-tweet-dash/) and the main [site](https://www.trafforddatalab.io/) <img src="data:image/png;base64,#img/session01/twitter_dash.PNG"/> --- # R to SQL connection [NHS-R Community Webinar](https://nhsrcommunity.com/learn-r/workshops/database-connections-in-r-webinar) <img src="data:image/png;base64,#img/session01/webinar_nhsr.PNG"/> --- class: center, middle # Inclusivity ![](data:image/png;base64,#01-workshop_intro_files/figure-html/inclusive-1.gif)<!-- --> --- class: inverse, middle, center .left-col[.center[ ## Course Philosophy ]] --- [Minimum Viable Product](https://blog.crisp.se/2016/01/25/henrikkniberg/making-sense-of-mvp) .left-col[.center[ <img src="data:image/png;base64,#img/session01/mvp.PNG" width="75%"/> ]] --- class: center, middle # Course philosophy Relaxed and informal Slides and code are available on [<svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> GitHub](https://github.com/nhs-r-community/intro_r) The truth; but it can’t be the whole truth… too much to cover in a day --- class: inverse, middle, center # Let's begin --- class: center, middle ## R vs. RStudio R is a programming language RStudio is a software application with tools to improve your programming experience --- <img class="center" src="data:image/png;base64,#img/session01/race_car.PNG"/> --- <img class="center" src="data:image/png;base64,#img/session01/roberto-nickson-unsplash.PNG"/> --- # RStudio Many excellent features to help you with your analyses. Never again have to think about R and RStudio as separate: Opening R Studio opens an R session. Analogy from the book Modern Dive: [www.moderndive.com](https://moderndive.com/) --- class: center, middle # Open RStudio R Studio opens an R session --- .left-column[ The Console is your window to R. You can code directly in the console… pi*2 <kbd> Enter </kbd> 37/12 <kbd> Enter <kbd> … but there is a better way… ] .right-column[ <img src="data:image/png;base64,#img/session01/rstudio_console.PNG"/> ] --- .left-column[ #### The Editor If you don’t see the Editor pane, click top right button And choose "R Script" from the drop down. Or, shortcut: <kbd> Ctrl + Shift + N </kbd> .blue[The cloud shortcut is <kbd> Ctrl + Shift + Alt + N </kbd>] ] .pull-right[ <img src="data:image/png;base64,#img/session01/new_file.PNG"/> ] --- .left-column[ The Editor is just like any other text editor: you can copy, paste, and save text. <kbd> Ctrl + Z </kbd> undoes *but* <kbd> Ctrl + Shift + Z </kbd> to redo (.blue[not <kbd> Ctrl + y</kbd>]) Different text is coloured (the console is uniform) Autocomplete <kbd>Ctrl + Enter </kbd> (sends line of code to Console) ] .right-column[ <img src="data:image/png;base64,#img/session01/editor.PNG"/> ] --- .left-column[ Comment code with a # E.g # this was a bad idea Comment frequently, at least in the beginning ] .right-column[ <img src="data:image/png;base64,#img/session01/editor.PNG"/> ] --- # Tools -> Options [Reasons why this is default](https://community.rstudio.com/t/defaults-of-saving-and-restoring-workspace/939) <img class="center" src="data:image/png;base64,#img/session01/global_options.PNG"/> --- # Tools -> Options Accessbility and comfort for all <img class="center" src="data:image/png;base64,#img/session01/appearance.PNG"/> --- class: center, middle # Packages ### R packages are like apps for your phone: -- Extend the capabilities of the basic or "base R" with extra functions, datasets, documentation. --- <img class="center" src="data:image/png;base64,#img/session01/app_analogy.PNG"/> --- # Packages Quotations in R can be either "" or '' but cannot be mixed: ```r # either install.packages("tidyverse") # or install.packages('tidyverse') ``` will download a package to your personal library. Then: ```r library(tidyverse) ``` This tells R to load the package from your personal library and is .blue[needed for every new session/script] --- class: center, middle ## CRAN repository [Comprehensive R Archive Network](https://cran.r-project.org/web/packages/) 18,903 packages (April 2022). Free. Peer reviewed. (Manifold possibilities) eg. interactive graphics and dashboards, machine learning, mine twitter data, create PowerPoint docs, maps… ## GitHub Many useful packages in development or subject to a lot of change are not on CRAN and are available through GitHub including [{NHSRtheme}](https://github.com/nhs-r-community/NHSRtheme). These won't be peer reviewed. ## ROpenSci ROpenSci offer a peer reviewed ecosystem of R packages through GitHub including UKHSA's [{fingertipsR}](https://github.com/ropensci/fingertipsR) --- <img class="center" src="data:image/png;base64,#img/session01/tidyverse.PNG"/> --- class: center, middle ## What is the tidyverse? The [tidyverse package](https://www.tidyverse.org/) collects (some of) the most popular R packages into one. All have the same underlying principles: Provide simple tools (with consistent structure) which may be used together to help solve complex problems. --- class: center, middle ## What is the tidyverse? During the workshop we will use the [ggplot2](https://ggplot2.tidyverse.org/), [dplyr](https://dplyr.tidyverse.org/), and [readr](https://readr.tidyverse.org/) packages. These are bundled up in the tidyverse package. Load it by running: ```r library(tidyverse) ``` --- # Output Information - what was loaded and potential conflicts ``` -- Attaching packages ------------------------------------------ tidyverse 1.3.0 -- v ggplot2 3.3.3 v purrr 0.3.4 v tibble 3.1.0 v dplyr 1.0.4 v tidyr 1.1.2 v stringr 1.4.0 v readr 1.4.0 v forcats 0.5.1 -- Conflicts --------------------------------------------- tidyverse_conflicts() -- x dplyr::filter() masks stats::filter() x dplyr::lag() masks stats::lag() ``` Warnings - not errors, just information on versions ``` Warning messages: 1: package ‘tidyverse’ was built under R version 4.0.4 2: package ‘tidyr’ was built under R version 4.0.3 3: package ‘readr’ was built under R version 4.0.3 4: package ‘purrr’ was built under R version 4.0.3 5: package ‘dplyr’ was built under R version 4.0.3 6: package ‘stringr’ was built under R version 4.0.3 ``` --- #### This work is licensed as Creative Commons Attribution ShareAlike 4.0 International To view a copy of this license, visit https://creativecommons.org/licenses/by/4.0/ --- name: goodbye class: middle, inverse # **Thank you!** Acknowledgements: for creating the original training slides and delivering training: Andrew Jones | Ozayr Mohammed Healthcare Analysts | The Strategy Unit [<svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M440 6.5L24 246.4c-34.4 19.9-31.1 70.8 5.7 85.9L144 379.6V464c0 46.4 59.2 65.5 86.6 28.6l43.8-59.1 111.9 46.2c5.9 2.4 12.1 3.6 18.3 3.6 8.2 0 16.3-2.1 23.6-6.2 12.8-7.2 21.6-20 23.9-34.5l59.4-387.2c6.1-40.1-36.9-68.8-71.5-48.9zM192 464v-64.6l36.6 15.1L192 464zm212.6-28.7l-153.8-63.5L391 169.5c10.7-15.5-9.5-33.5-23.7-21.2L155.8 332.6 48 288 464 48l-59.4 387.3z"></path></svg> andrew.jones40@nhs.net](mailto:andrew.jones40@nhs.net) [<svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M440 6.5L24 246.4c-34.4 19.9-31.1 70.8 5.7 85.9L144 379.6V464c0 46.4 59.2 65.5 86.6 28.6l43.8-59.1 111.9 46.2c5.9 2.4 12.1 3.6 18.3 3.6 8.2 0 16.3-2.1 23.6-6.2 12.8-7.2 21.6-20 23.9-34.5l59.4-387.2c6.1-40.1-36.9-68.8-71.5-48.9zM192 464v-64.6l36.6 15.1L192 464zm212.6-28.7l-153.8-63.5L391 169.5c10.7-15.5-9.5-33.5-23.7-21.2L155.8 332.6 48 288 464 48l-59.4 387.3z"></path></svg> ozayr.mohammed@nhs.net](mailto:ozayr.mohammed@nhs.net) And to Silvia Canelón who created the Xaringan presentation using NHS and NHR-R colour branding and shared this at the 2020 [NHS-R Community conference](https://spcanelon.github.io/xaringan-basics-and-beyond/index.html). Details of the workshops she ran at the [NHS-R Community conference](https://spcanelon.github.io/xaringan-basics-and-beyond/index.html). And to Zoë Turner who converted the presentation to Xaringan. [<svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M440 6.5L24 246.4c-34.4 19.9-31.1 70.8 5.7 85.9L144 379.6V464c0 46.4 59.2 65.5 86.6 28.6l43.8-59.1 111.9 46.2c5.9 2.4 12.1 3.6 18.3 3.6 8.2 0 16.3-2.1 23.6-6.2 12.8-7.2 21.6-20 23.9-34.5l59.4-387.2c6.1-40.1-36.9-68.8-71.5-48.9zM192 464v-64.6l36.6 15.1L192 464zm212.6-28.7l-153.8-63.5L391 169.5c10.7-15.5-9.5-33.5-23.7-21.2L155.8 332.6 48 288 464 48l-59.4 387.3z"></path></svg> zoe.turner2@notthshc.nhs.uk](mailto:zoe.turner2@nottshc.nhs.uk)