How to permanently set R working directory

 

 

 

 Using R Studio

If you are using RStudio for R Programming just go to Tools -> Global Options... -> General one can set the default working directory. Also under Session one can Set Working Directory To Source File Location and To Files Pane Location with single click and with a graphical tool to any directory.

Setting a convenient default is of course useful, but at least I have at most one project (in general meaning of the word and not just in R meaning of the word) in one folder so I have to switch the working directory anyway between projects.


Using  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()

Comments

Popular posts from this blog

Osmania university PGECET counselling 2011 schedule

I Registered TGMC 2010

Ancient indian maps