1
Getting used to using R Tips
Workshop 2
1
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
2
By doing this workshop and carrying out the independent study the successful student will be able to:
Miscellaneous and practice
3
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
> getwd() [1] "M:/web/58M - 2017.yrk/pracs" > dir() [1] "00PrecourseR.html" [2] "00PrecourseR.Rmd" [3] "01GettingUsedToUsingR.html" [4] "01GettingUsedToUsingR.Rmd" ………………….
5
> dir("../data") [1] "1_trace1" "beewing.txt" [3] "biomass.txt" "carsdata.dta" [5] "clusters.txt" "csativa.txt" [7] "fungi.txt" "Gain.txt"
6
> 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" ..
7
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")
8
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
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
magrittr package The magrittr package offers a set of operators which make your code more readable by:
inside and out),
11 11
> 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
By doing this workshop and carrying out the independent study the successful student will be able to:
Miscellaneous and practice