Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change directory in R

Tags:

r

I want to be able to change the directory I am working in whilst using the R command line interface. I do not want to change the working directory but only change the directory temporarily in much the same way one changes directories by using "cd".

Can this be done and if so how do I do this?

like image 782
radiobrain77 Avatar asked Aug 08 '15 03:08

radiobrain77


2 Answers

setwd('path')

Just stick the path in and you're done.

like image 66
CinchBlue Avatar answered Sep 30 '22 19:09

CinchBlue


R setwd is equivalent to the shell cd. If you have 3 subdirectories (dirA, dirB, and dirC) and you want to do some work inside the directory you need to write the following code:

dirs=c("dirA", "dirB", "dirC") # normally you get these by some means
# not through hard-coding; here is for example only
pwd <- getwd()
for (d in dirs) {
   setwd(d)
   # do some work in the subdirectory
   setwd(pwd)  # return to parent directory
}
like image 42
Kemin Zhou Avatar answered Sep 30 '22 21:09

Kemin Zhou