getting used to using r tips
play

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


  1. 1 Getting used to using R Tips Workshop 2 1

  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

  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

  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" ………………… .

  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"

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

  7. 7 Setting wd to source file without menu x <- rstudioapi::getActiveDocumentContext()$path install.packages("rstudioapi") 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)

  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

  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

  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.

  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

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend