Getting used to using R Tips Workshop 2 1 2 Objectives By doing - - PowerPoint PPT Presentation

getting used to using r tips
SMART_READER_LITE
LIVE PREVIEW

Getting used to using R Tips Workshop 2 1 2 Objectives By doing - - PowerPoint PPT Presentation

1 Getting used to using R Tips Workshop 2 1 2 Objectives By doing this workshop and carrying out the independent study the successful student will be able to: work with a variety of directory structures using relative paths be


slide-1
SLIDE 1

1

Getting used to using R Tips

Workshop 2

1

slide-2
SLIDE 2

2

Objectives

By doing this workshop and carrying out the independent study the successful student will be able to:

  • work with a variety of directory structures using relative paths
  • be able to use some standard housekeeping commands
  • be able to use the pipe operator %>%
  • some some strategies for Problem solving trouble shooting / debugging

Miscellaneous and practice

slide-3
SLIDE 3

3

Paths - you started to encounter yesterday

Absolute path or Full path the location of a filesystem object (i.e., file, directory or link) relative to the root directory.

Windows: M:/web/58M - 2017.yrk/data/beewing.txt Linux : /users/er13/web/58M - 2017.yrk/data/beewing.txt Web : http://www-users.york.ac.uk/~er13/58M%20-%202017.yrk/data/beewing.txt

Relative path the location of a filesystem object relative to the working directory.

Same directory : beewing.txt Directory above : ../beewing.txt In a directory called data in the wd : data/beewing.txt In a directory called data in the directory above : ../data/beewing.txt

3

slide-4
SLIDE 4

4

Directory structure and relative paths

> getwd() [1] "M:/web/58M - 2017.yrk/pracs" > dir() [1] "00PrecourseR.html" [2] "00PrecourseR.Rmd" [3] "01GettingUsedToUsingR.html" [4] "01GettingUsedToUsingR.Rmd" ………………….

slide-5
SLIDE 5

5

Directory structure and relative paths

> dir("../data") [1] "1_trace1" "beewing.txt" [3] "biomass.txt" "carsdata.dta" [5] "clusters.txt" "csativa.txt" [7] "fungi.txt" "Gain.txt"

slide-6
SLIDE 6

6

Directory structure and relative paths

> dat <- read.table("beewing.txt") Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file 'beewing.txt': No such file or directory > dat <- read.table("data/beewing.txt") Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file 'data/beewing.txt': No such file or directory > dat <- read.table("../data/beewing.txt") > > getwd() [1] "M:/web/58M - 2017.yrk/pracs" > dir() [1] "00PrecourseR.html" [2] "00PrecourseR.Rmd" [3] "01GettingUsedToUsingR.html" [4] "01GettingUsedToUsingR.Rmd" ..

slide-7
SLIDE 7

7

Setting wd to source file without menu

x <- rstudioapi::getActiveDocumentContext()$path y <- unlist(strsplit(x, "/")) y <- y[-length(y)] path <- paste(y, collapse="/") setwd(path) > x <- rstudioapi::getActiveDocumentContext()$path > x [1] "Z:/My Documents/MBiol/reproducible/reproducible/pracs/mywork.R" > y <- unlist(strsplit(x, "/")) > y [1] "Z:" "My Documents" "MBiol" "reproducible" "reproducible" [6] "pracs" "mywork.R" > y <- y[-length(y)] > y [1] "Z:" "My Documents" "MBiol" "reproducible" "reproducible" [6] "pracs" > path <- paste(y, collapse="/") > path [1] "Z:/My Documents/MBiol/reproducible/reproducible/pracs" > setwd(path) install.packages("rstudioapi")

slide-8
SLIDE 8

8

Typical Errors

Error in ggplot(perisummary, aes(x = season, y = para, fill = species)) : could not find function "ggplot" Is package installed? Have you done library() Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file 'data/beewing.txt': No such file or directory Have not used the correct relative path > ggplot(perisummary, aes(x = season, y = para, shape = species) ) + + geom_point(size = 2, position = position_dodge(0.9))+ + scale_shape_manual(values = c(1, 19)) + + + Help the cursor won’t return! R thinks the command is still running. You might have missed a bracket, or quote. Add what is required. You’ll get an error but the prompt will return

slide-9
SLIDE 9

9

Google and more!

Google the error message! Google well defined questions “how do a t-test in R” http://www.cookbook-r.com/ http://r4ds.had.co.nz/ http://www-users.york.ac.uk/~er13/ Look at the image results. Use Find and Replace

slide-10
SLIDE 10

10 10

The pipe: %>%

magrittr package The magrittr package offers a set of operators which make your code more readable by:

  • structuring sequences of data operations left-to-right (as opposed to from the

inside and out),

  • avoiding nested function calls,
  • minimizing the need for local variables and function definitions,
  • making it easy to add steps anywhere in the sequence of operations.
slide-11
SLIDE 11

11 11

Nesting functions and intermediate variables

> dat <- 1:10 > myss <- function(v) {sum((v - mean(v))^2)} > myss(dat) [1] 82.5 > library(magrittr) > myss2 <- function(v) {(v - mean(v))^2 %>% sum} > myss2(dat) [1] 82.5 > obs <- c(4, 13, 14, 15, 13, 5) > exp <- c(2, 10, 20, 20, 10, 2) > chi <- sum((obs-exp)^2 / exp) > pchisq(chi,5,lower.tail=F) [1] 0.04486554 > sum((obs-exp)^2 / exp) %>% pchisq(.,5,lower.tail=F) [1] 0.04486554

slide-12
SLIDE 12

12

Objectives

By doing this workshop and carrying out the independent study the successful student will be able to:

  • work with a variety of directory structures using relative paths
  • be able to use some standard housekeeping commands
  • be able to use the pipe operator %>%
  • some some strategies for Problem solving trouble shooting / debugging

Miscellaneous and practice