getwd: Get or Set Working Directory

Description Usage Arguments Value Note See Also Examples


getwd returns an absolute filepath representing the current working directory of the R process; setwd(dir) is used to set the working directory to dir.





A character string: tilde expansion will be done.


getwd returns a character string or NULL if the working directory is not available. On Windows the path returned will use / as the path separator and be encoded in UTF-8. The path will not have a trailing / unless it is the root directory (of a drive or share on Windows).

setwd returns the current directory before the change, invisibly and with the same conventions as getwd. It will give an error if it does not succeed (including if it is not implemented).


Note that the return value is said to be an absolute filepath: there can be more than one representation of the path to a directory and on some OSes the value returned can differ after changing directories and changing back to the same directory (for example if symbolic links have been traversed).

See Also

list.files for the contents of a directory.

normalizePath for a ‘canonical’ path name.


(WD <- getwd())
if (!is.null(WD)) setwd(WD)

Questions? Problems? Suggestions? or email at

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.