How can I determine the current directory name in R?

M. Tibbits picture M. Tibbits · Apr 26, 2011 · Viewed 14.3k times · Source

The only solution I've encountered is to use regular expressions and recursively replace the first directory until you get a word with no slashes.

gsub("/\\w*/","/",gsub("/\\w*/","/",getwd()))

Is there anything slightly more elegant? (and more portable?)

Answer

Joshua Ulrich picture Joshua Ulrich · Apr 27, 2011

Your example code doesn't work for me, but you're probably looking for either basename or dirname:

> getwd()
[1] "C:/cvswork/data"
> basename(getwd())
[1] "data"
> dirname(getwd())
[1] "C:/cvswork"