+ - 0:00:00
Notes for current slide
Notes for next slide

Introduction to R and Rstudio


Session 1

1 / 37

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

Finish about 4 - 430pm

2 / 37

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

(Excellent free resources available)

3 / 37

elegant

Graphics

4 / 37

5 / 37

The original blog is no longer available that featured this

6 / 37

Collaboration

Reproducibility

R Markdown

7 / 37

8 / 37

(Interactive) Dashboards

Mental Health Surge Modelling

9 / 37

Trafford Data Lab and the main site

10 / 37

R to SQL connection

NHS-R Community Webinar

11 / 37

Inclusivity

12 / 37

Course Philosophy

13 / 37

Course philosophy

Relaxed and informal

Slides and code are available on GitHub

The truth; but it can’t be the whole truth…
too much to cover in a day

15 / 37

Let's begin

16 / 37

R vs. RStudio

R is a programming language

RStudio is a software application with tools to

improve your programming experience

17 / 37

18 / 37

19 / 37

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

20 / 37

Open RStudio

R Studio opens an R session

21 / 37

The Console is your window to R.

You can code directly in the console…

pi*2 Enter
37/12 Enter
… but there is a better way…

22 / 37

The Editor

If you don’t see the Editor pane, click top right button

And choose "R Script" from the drop down.

Or, shortcut:
Ctrl + Shift + N

The cloud shortcut is Ctrl + Shift + Alt + N

23 / 37

The Editor is just like any other text
editor: you can copy, paste, and save text.

Ctrl + Z undoes but
Ctrl + Shift + Z to redo
(not Ctrl + y)

Different text is coloured
(the console is uniform)

Autocomplete

Ctrl + Enter
(sends line of
code to Console)

24 / 37

Comment code with a #

E.g # this was a bad idea

Comment frequently, at least in the beginning

25 / 37

Tools -> Options

Reasons why this is default

26 / 37

Tools -> Options

Accessbility and comfort for all

27 / 37

Packages

R packages are like apps for your phone:

28 / 37

Packages

R packages are like apps for your phone:

Extend the capabilities of the basic or "base R"
with extra functions, datasets, documentation.

28 / 37

29 / 37

Packages

Quotations in R can be either "" or '' but cannot be mixed:

# either
install.packages("tidyverse")
# or
install.packages('tidyverse')

will download a package to your personal library. Then:

library(tidyverse)

This tells R to load the package from your personal library and is needed for every new session/script

30 / 37

CRAN repository

Comprehensive R Archive Network

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}. These won't be peer reviewed.

ROpenSci

ROpenSci offer a peer reviewed ecosystem of R packages through GitHub including UKHSA's {fingertipsR}

31 / 37

32 / 37

What is the tidyverse?

The tidyverse package 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.

33 / 37

What is the tidyverse?

During the workshop we will use the ggplot2, dplyr,
and readr packages. These are bundled up in the
tidyverse package. Load it by running:

library(tidyverse)
34 / 37

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
35 / 37

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/

36 / 37

Thank you!

Acknowledgements: for creating the original training slides and delivering training:

Andrew Jones | Ozayr Mohammed
Healthcare Analysts | The Strategy Unit

andrew.jones40@nhs.net

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.

Details of the workshops she ran at the NHS-R Community conference.

And to Zoë Turner who converted the presentation to Xaringan. zoe.turner2@notthshc.nhs.uk

37 / 37

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

Finish about 4 - 430pm

2 / 37
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow