2015-08-08 75 views
1

我希望能够在使用R命令行界面的同时更改我所在的目录。我不想改变工作目录,只是暂时改变目录,就像用“cd”改变目录一样。更改R中的目录

可以这样做,如果是的话,我该怎么做?

回答

3

setwd('path')

只要坚持路径,你就大功告成了。

+0

这是否永久地改变工作目录? – radiobrain77

+0

不,这不是永久的。如果需要,您可以再次更改目录 - 即使在同一个会话中。 – Whitebeard

0

R setwd相当于shell cd。如果你有3个子目录(DIRA,DIRB和DIRC),你想你需要编写如下代码目录内的一些工作:

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 
}