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

Getting started with
R and RStudio

RStudio
1 / 33
R logo

The engine

RStudio logo

The dashboard

2 / 33

A tour of RStudio

3 / 33
RStudio
4 / 33

Console

RStudio console

R is awaiting your instructions

Type code here, press enter, and R will run it

5 / 33

Your turn

RStudio console

Type 2 + 2 in the console

Press enter

6 / 33
2 + 2
## [1] 4

This is ephemeral though.
If you want to run this again, you'll have to type it again.

Store R code in a document instead

7 / 33

Files pane

RStudio files panel

All the files in your current working directory

8 / 33

Your turn

RStudio files pane

Find 01_getting-started.Rmd

Click on its name to open the file

9 / 33

Source pane

RStudio source panel

Documents
open here

10 / 33

R Markdown

RStudio R Markdown document

Document format that
combines text and code

Acts like a notebook
for your analysis

11 / 33

R Markdown

RStudio R Markdown text

Text

12 / 33

R Markdown

RStudio R Markdown code

Text

Code

13 / 33

R Markdown

RStudio R Markdown output

Text

Code

Output

14 / 33

Your turn

RStudio R Markdown code

Read the instructions

Run the code chunk by
clicking the play button

15 / 33

Your turn

Add a new chunk

Put 2 + 2 in the chunk and run it

16 / 33

Knitting

"Knit" an R Markdown document into a standalone sharable file

RStudio knit button
17 / 33

R Markdown

The best way to combine R code and narrative

We'll use it throughout the class:

I'll provide starter code

You'll complete "Your turns"

In the end, you'll have an annotated record for yourself

18 / 33

Your turn

Spot the difference:

filter(mtcars, cyl == 4)
four_cyls <- filter(mtcars, cyl == 4)

Find these chunks in the notebook and run them.
What's different about what happens?

19 / 33

Assignment

<- assigns the output from the righthand side to a variable with the name on the lefthand side

four_cyls <- filter(mtcars, cyl == 4)
20 / 33

Environment pane

RStudio environment pane

List of all the
variables you've created

21 / 33

Your turn

Find four_cyls in the environment pane.
Click on the name four_cyls

What happens?

22 / 33

Viewer

RStudio viewer

Clicking on an object in the environment panel opens it an interactive viewer tab

23 / 33

Functions

four_cyls <- filter(mtcars, cyl == 4)

Functions do things

24 / 33

Functions

four_cyls <- filter(mtcars, cyl == 4)

Functions do things

Functions take arguments, output results

24 / 33

Functions

four_cyls <- filter(mtcars, cyl == 4)

Functions do things

Functions take arguments, output results

If you want to keep the output, assign it to a variable

24 / 33

Help

To look up the help page for an R function,
type this in the console:

?function_name

(Or google it!)

25 / 33

Help pane

RStudio help pane

These help pages prove details about the arguments you can supply a function

Often full of examples
at the bottom

26 / 33

Your turn

Look at the help page for seq

Add a chunk that uses seq() to create a
list of numbers from 5 to 100, spaced by 5
(so 5, 10, 15, 20, …)

02:00
27 / 33
seq(from = 5, to = 100, by = 5)
## [1] 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95
## [20] 100
28 / 33

Common syntax problem #1

Missing closing parentheses or quotes

mean(mtcars
"Oops this is wrong
29 / 33

Common syntax problem #2

Surrounding something in quotes when it should be (or vice versa)

mean("mtcars")
## Warning in mean.default("mtcars"): argument is not numeric or logical: returning
## NA
## [1] NA
30 / 33

Your turn

There are three chunks under "Syntax gone wrong"

Run each, read the error message, and try to fix the syntax

31 / 33

Cheatsheets

Go to Help > Cheatsheets to find quick
reference guides to different packages

RStudio cheatsheet
32 / 33

Next up

Data basics

33 / 33
R logo

The engine

RStudio logo

The dashboard

2 / 33
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
oTile View: Overview of Slides
Esc Back to slideshow