GET UP AND RUNNING TODAYS GOAL: GET ORIENTED TO RSTUDIO, MAKE A - - PowerPoint PPT Presentation

get up and running
SMART_READER_LITE
LIVE PREVIEW

GET UP AND RUNNING TODAYS GOAL: GET ORIENTED TO RSTUDIO, MAKE A - - PowerPoint PPT Presentation

GET UP AND RUNNING TODAYS GOAL: GET ORIENTED TO RSTUDIO, MAKE A PROJECT FOLDER https://www.r-project.org https://rstudio.com RStudio will be the control center for your R Projects >_ Current Objects Document Files, Graphs,


slide-1
SLIDE 1

GET UP AND RUNNING

slide-2
SLIDE 2

TODAY’S GOAL:

GET ORIENTED TO RSTUDIO, MAKE A PROJECT FOLDER

slide-3
SLIDE 3

https://www.r-project.org https://rstudio.com

slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6
slide-7
SLIDE 7
slide-8
SLIDE 8 >_

RStudio will be the control center for your R Projects

slide-9
SLIDE 9 >_

Current Document

Console Objects

Files, Graphs, Help

slide-10
SLIDE 10 > if (is.empty.model(mt)) { x <- NULL z <- list(coefficients = if (mlm) matrix(NA_real_, 0, ncol(y)) else numeric(), residuals = y, fitted.values = 0 * y, weights = w, rank = 0L, df.residual = if (!is.null(w)) sum(w != 0) else ny)

Drives R Produces Plots Generates Documents RStudio

slide-11
SLIDE 11

THE RIGHT FRAME OF MIND

slide-12
SLIDE 12

TYPE OUT YOUR

CODE BY HAND

slide-13
SLIDE 13

RSTUDIO AGAIN

slide-14
SLIDE 14 install.packages("usethis", repos = "http://cran.rstudio.com")

Paper, Report, Analysis, Notes, etc, in RMarkdown

slide-15
SLIDE 15 install.packages("usethis", repos = "http://cran.rstudio.com")

Console: Type or send code here, see results

slide-16
SLIDE 16 install.packages("usethis", repos = "http://cran.rstudio.com")

Project files, Plots, Help

slide-17
SLIDE 17 install.packages("usethis", repos = "http://cran.rstudio.com")

Inspect objects you create

slide-18
SLIDE 18

TASK:

CREATE A PROJECT FOR YOUR NOTES

slide-19
SLIDE 19

Use RMarkdown TO PRODUCE & REPRODUCE YOUR OWN WORK

slide-20
SLIDE 20

This is what we want to end up with: nicely formatted text, plots, and tables in an HTML, PDF,

  • r Word file
  • 1. Lorem Ipsum
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enimad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
slide-21
SLIDE 21

In a Literate Programming approach to documents, chunks of code are processed and replaced with their output

library(ggplot2) tea <- rnorm(100) biscuits <- tea + rnorm(100, 0, 1.3) data <- data.frame(tea, biscuits) p <- ggplot(data, aes(x = tea, y = biscuits)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Tea", y = "Biscuits") + theme_bw() print(p) # Lorem Ipsum Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do *eiusmod tempor* incididunt ut labore et dolore magna
  • aliqua. Ut enimad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
  • ccaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.

This is what our file actually looks like: plain text, mixing words and chunks of code

slide-22
SLIDE 22

Markdown is a way to put formatting instructions in plain-text documents

# Header Plain text *italics* **bold** `verbatim` Footnote.[^1] [^1]: The footnote.
  • 1. List
  • 2. List
  • Bullet 1
  • Bullet 2
!" Subhead Markdown Output Header Plain text italics bold verbatim Footnote. The footnote.
  • 1. List
  • 2. List
° Bullet 1 ° Bullet 2 Subhead 1 1

Once the chunks of R Code have all been run, a Markdown Processor turns the bits of marked-up plain text into properly-formatted

  • utput in HTML, PDF, DOCX or
  • ther file types.

What you type What you end up with

slide-23
SLIDE 23

In a Literate Programming approach to documents, chunks of code are processed and replaced with their output

library(ggplot2) tea <- rnorm(100) biscuits <- tea + rnorm(100, 0, 1.3) data <- data.frame(tea, biscuits) p <- ggplot(data, aes(x = tea, y = biscuits)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Tea", y = "Biscuits") + theme_bw() print(p) # Lorem Ipsum Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do *eiusmod tempor* incididunt ut labore et dolore magna
  • aliqua. Ut enimad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
  • ccaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
slide-24
SLIDE 24
  • 1. Lorem Ipsum
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enimad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

In a Literate Programming approach to documents, chunks of code are processed and replaced with their output

slide-25
SLIDE 25

An Rmd document lets you keep your code and notes together in plain text And produce good-looking

  • utput in a range of formats
slide-26
SLIDE 26

An Rmd document lets you keep your code and notes together in plain text And produce good-looking

  • utput in a range of formats
knit in R notes.Rmd # Report We can see this *relationship* in a scatterplot. As you can see, this plot looks pretty nice. Report We can see this relationship in a scatterplot. As you can see, this plot looks pretty nice. x y notes.html ```{r my-code} p !" ggplot(data, mapping) p + geom_point() ```
slide-27
SLIDE 27

An Rmd document lets you keep your code and notes together in plain text And produce good-looking

  • utput in a range of formats
knit in R notes.Rmd # Report We can see this *relationship* in a scatterplot. As you can see, this plot looks pretty nice. Report We can see this relationship in a scatterplot. As you can see, this plot looks pretty nice. x y notes.docx ```{r my-code} p !" ggplot(data, mapping) p + geom_point() ```
slide-28
SLIDE 28

Header section provides metadata and sets options

Code chunk

Text with Markdown formatting In RStudio, code chunks can be "played" one at a time Chunks are replaced by their
  • utput when the
document is made

Code chunks can have their

  • wn names and options
slide-29
SLIDE 29

RStudio will do all the work for you when it comes to processing your document—i.e., getting it from plain-text Rmd to HTML, Word, or PDF.

  • 1. Lorem Ipsum
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enimad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
slide-30
SLIDE 30

In general, your code is what’s “real” in your project, not the objects you create.

slide-31
SLIDE 31

Consider not showing your

  • utput inline
slide-32
SLIDE 32

GETTING ORIENTED

slide-33
SLIDE 33 library(tidyverse) Loading tidyverse: ggplot2 Loading tidyverse: tibble Loading tidyverse: tidyr Loading tidyverse: readr Loading tidyverse: purrr Loading tidyverse: dplyr

The Tidyverse

Draw graphs Nicer data tables Tidy your data Get data into R Cool functional programming stuff Action verbs for manipulating data library(socviz)

Course-Specific Library

slide-34
SLIDE 34

CODE YOU CAN TYPE AND RUN

## Inside chunks of code, lines beginning with ## the hash character are comments my_numbers <- c(1, 1, 4, 1, 1, 4, 1) my_numbers ## [1] 1 1 4 1 1 4 1

OUTPUT

What R Looks Like

slide-35
SLIDE 35

FOUR THINGS TO KNOW ABOUT R

slide-36
SLIDE 36

1: Everything has a Name

FALSE TRUE Inf for if break function

Some names are forbidden

my_numbers data p
slide-37
SLIDE 37
  • 2. Everything is an Object
my_numbers <- c(1, 2, 3, 1, 3, 5, 25) > letters ## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" [20] "t" "u" "v" "w" "x" "y" "z"

You create objects by assigning a thing to a name

named thing "gets" this stuff

slide-38
SLIDE 38 my_numbers <- c(1, 2, 3, 1, 3, 5, 25)

You create objects by assigning a thing to a name

The assignment operator performs the action of creating objects. Use a keyboard shortcut to type it:

  • ption - Mac

alt - Windows

slide-39
SLIDE 39
  • 3. You do things with named objects

using functions and operators

my_numbers <- c(1, 2, 3, 1, 3, 5, 25)

named thing "gets" this stuff c() is a function that takes comma-separated numbers or strings and joins them together into a vector

slide-40
SLIDE 40

take arguments, perform actions, produce outputs

mean()

Functions have parentheses at the end of their name. This is where the inputs,

  • r arguments go.
mean(x = my_numbers)

Named argument. “Calculate the mean of what, please?” These names are internal to functions. “Take this object …”

Functions

slide-41
SLIDE 41 mean(my_numbers)

If you just write the name of the input, R assigns it to the function’s arguments in order. Look at the function’s help page to see the order it expects its arguments.

take arguments, perform actions, produce outputs

Functions

slide-42
SLIDE 42

You can assign a function’s

  • utput to a named object
my_summary <- summary(my_numbers) my_sd <- sd(my_numbers) my_summary my_sd
slide-43
SLIDE 43

Objects you create exist until you overwrite or delete them

rm(my_numbers) my_numbers my_numbers <- c(1, 2, 3, 1, 3, 5, 25)
slide-44
SLIDE 44

Objects are of different classes

class(my_numbers) numeric character factor

Vectors

matrix data.frame tibble

Arrays

lm glm

Models

slide-45
SLIDE 45

Things to try on Objects

class(my_numbers) table(my_numbers) x <- c(my_numbers, 5) mean(c(my_numbers, my_numbers))

Notice that these are functions How do x and y differ?

y <- c(my_numbers, "hello")

Functions can be nested, and will be evaluated from the inside out.

slide-46
SLIDE 46

Some operators

+, -, *, /, ^

Arithmetic

<-

Assignment ("gets")

=

  • r

&, &&, |, ||, !

Logical

%*%, %in%, %>% Special <, >, <=, >=, ==, !=

Relational

slide-47
SLIDE 47

The pipe operator

mean(my_numbers) my_numbers %>% mean()

This will be very convenient later on

round(mean(my_numbers)) my_numbers %>% mean() %>% round()

"and then"

%>%

slide-48
SLIDE 48
  • 4. R will be Frustrating

We’re going to be joining a lot of objects and functions together

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point()

"+"

goes here

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point()

not here

slide-49
SLIDE 49
  • 4. R will be Frustrating

RStudio tries its best to help. Learn to attend to what it’s trying to tell you

slide-50
SLIDE 50

LET’S GO

slide-51
SLIDE 51 library(gapminder) gapminder # A tibble: 1,704 x 6 country continent year lifeExp pop gdpPercap <fctr> <fctr> <int> <dbl> <int> <dbl> 1 Afghanistan Asia 1952 28.801 8425333 779.4453 2 Afghanistan Asia 1957 30.332 9240934 820.8530 3 Afghanistan Asia 1962 31.997 10267083 853.1007 4 Afghanistan Asia 1967 34.020 11537966 836.1971 5 Afghanistan Asia 1972 36.088 13079460 739.9811 6 Afghanistan Asia 1977 38.438 14880372 786.1134 7 Afghanistan Asia 1982 39.854 12881816 978.0114 8 Afghanistan Asia 1987 40.822 13867957 852.3959 9 Afghanistan Asia 1992 41.674 16317921 649.3414 10 Afghanistan Asia 1997 41.763 22227415 635.3414 # ... with 1,694 more rows
slide-52
SLIDE 52 p + geom_point() p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) p

Named thing gets … … the output of this function … … using these arguments Objects created by ggplot() are unusual in that you can “add” things to them, and they will work as though you wrote all the code at once.

slide-53
SLIDE 53 p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) p + geom_point()
slide-54
SLIDE 54 40 60 80 30000 60000 90000 gdpPercap lifeExp
slide-55
SLIDE 55

NEXT

WEEK

slide-56
SLIDE 56
  • 1. Readings will be provided

in the Slack

  • 3. Finish setting up your

notes.Rmd file. Use it to take notes on the reading

  • 4. Questions or glitches with

R or RStudio: Ask in the Slack

  • 2. Slides will be on the

Website