getwd, setwd

R is always pointed at a directory on your computer. You can find out which directory by running the getwd (get working directory) function; this function has no arguments. To change your working directory, use setwd and specify the path to the desired folder.
getwd()
setwd(dir)
  • dir – Specify a working directory. This may be from the root directory (starting with / on a Mac), it may include a double-dot (..) to move locally up a folder from the current directory, and it may include a path from the current directory.

Example. In the first block of code, I verify there is nothing in the work space, I check the current directory, and I specify a directory I want to go to (getting to my Videos folder), navigate further to the subfolder packageFiles. Finally, I source in a file from this folder, which creates the lmPlot function in my workspace. The last two commands show how easy it is to move up one folder.
> ls()
character(0)
> getwd()
[1] "/Users/ddiez"
> setwd("/Users/ddiez/Dropbox/rFunction/Videos")
> setwd("packageFiles")
> getwd()
[1] "/Users/ddiez/Dropbox/rFunction/Videos/packageFiles"
> 
> source("lmPlot.R")
> ls()
[1] "lmPlot"
> 
> setwd("..")
> getwd()
[1] "/Users/ddiez/Dropbox/rFunction/Videos"

Leave a Reply