Chapter 9 Working Directory

Use getwd to query the current working directory. To change working directory, use setwd. For Windows user, the path separator is typically the back-slash \. But it is the escape character in R so if one would like to use back-slash it must be escaped, for example, like this:

setwd("C:\\Users\\kylechung")

or equivalently one could still follow *nix convention:

setwd("C:/Users/kylechung")

One can use the function dir to return all the files and folders under the current directory:

# the content of this book's working directory
dir()
##  [1] "_book"                  "_bookdown.yml"         
##  [3] "_output.yml"            "01-installation.Rmd"   
##  [5] "02-cli-vs-ide.Rmd"      "03-data-struct.Rmd"    
##  [7] "03a-string.Rmd"         "04-control-flow.Rmd"   
##  [9] "05-function.Rmd"        "06-r-lang-char.Rmd"    
## [11] "07-working-env.Rmd"     "08-data-processing.Rmd"
## [13] "09-error-handle.Rmd"    "10-parallel.Rmd"       
## [15] "11-s4.Rmd"              "12-unittest.Rmd"       
## [17] "13-cowork.Rmd"          "14-rcpp.Rmd"           
## [19] "20-references.Rmd"      "book.bib"              
## [21] "bookdown-demo.Rproj"    "bookdown-demo.tex"     
## [23] "index.Rmd"              "LICENSE"               
## [25] "outline"                "packages.bib"          
## [27] "preamble.tex"           "rbasic101_main.Rmd"    
## [29] "README.md"              "samplecodes"           
## [31] "style.css"              "toc.css"

dir can also have an argument path pointing to whereever one would like to examine the file contents. The default value of path is ., which is the notation indicating current directory. One can also use relative path notation: .. to detnote the parent directory of current directory.

9.1 Library Search Path

Use .libPaths to show the path(es) that will be searched by R.

.libPaths()
## [1] "/Library/Frameworks/R.framework/Versions/3.2/Resources/library"

9.2 Save / Load

Use save to save serialized .RData for one variable. Or use save.image to save the entire working environment.