1
Developing independence and good practice - Tips Workshop 1 2 - - PowerPoint PPT Presentation
Developing independence and good practice - Tips Workshop 1 2 - - PowerPoint PPT Presentation
1 Developing independence and good practice - Tips Workshop 1 2 Outline Learning outcomes Brief talk covering ideas that will help in the workshop Directory structure, working directories, relative and absolute paths Housekeeping A few
2
Outline
Learning outcomes Brief talk covering ideas that will help in the workshop Directory structure, working directories, relative and absolute paths Housekeeping A few common error messages The pipe %>% Workshop Exercise in housekeeping, problem solving and googling An introduction to writing functions We should have plenty of time
2
3
Learning outcomes
By following the slides and applying the techniques to the workshop examples the successful student will be able to:
- rganise their work using directory structures and relative paths
- carry out standard housekeeping
- use the pipe operator %>%
- define their own functions
- use some strategies for troubleshooting and debugging
4
Paths
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 - 2018.yrk/data/beewing.txt Linux : /users/er13/web/58M - 2018.yrk/data/beewing.txt Web : http://www-users.york.ac.uk/~er13/58M%20-%202018.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 the wd ../data/beewing.txt
4
5
Directory structure and relative paths
> getwd() [1] "M:/web/58M - 2018.yrk/pracs" > dir() [1] "01DevelopingIndependenceAndGoodPracticeTips.html" [2] "01DevelopingIndependenceAndGoodPracticeTips.Rmd" [3] "02ImportingData.Rmd" ………………….
6
Directory structure and relative paths
> getwd() [1] "M:/web/58M - 2018.yrk/pracs" > dir("../data") [1] "1_trace1" [2] "2zta.pdb" [3] "beewing.txt" [4] "biomass.txt" [5] "carsdata.dta" [6] "CDK_FASTA.txt" ….
7
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 - 2018.yrk/pracs" > dir() [1] "01DevelopingIndependenceAndGoodPracticeTips.html" [2] "01DevelopingIndependenceAndGoodPracticeTips.Rmd" [3] "02ImportingData.Rmd"
8
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")
9
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
10 10
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
11 11
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.
12 12
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
13 13
Setting wd to source file without menu with the pipe
path <- rstudioapi::getActiveDocumentContext()$path %>% strsplit("/") %>% unlist() %>% .[-length(.)] %>% paste(., collapse = "/") setwd(path) getwd() x <- rstudioapi::getActiveDocumentContext()$path y <- unlist(strsplit(x, "/")) y <- y[-length(y)] path <- paste(y, collapse="/") setwd(path)
With the pipe Without the pipe