Get or Set Working Directory

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.

  • getwd
  • setwd
library(base) # NOT RUN { (WD <- getwd()) if (!is.null(WD)) setwd(WD) # }
Documentation reproduced from package base, version 3.6.1, License: Part of R 3.6.1

Community examples at May 1, 2019 base v3.6.0

funkcia1=function(a,b,n) for (i in 1:n) { if(i%%a == 0){ print(i/a) } else if(i%%b==0){ print("B") } else{ print(i) } } funkcia1(4,7,18) getwd() dir.create("Zap1B") setwd("Zap1B") library(XML) install.packages("XML") s=htmlParse("") tabulky= readHTMLTable(s,stringAsFactors=FALSE) timy=tabulky[1][c(2,3,8,4)] names(timy)=c("tim","zapasy","skore","body") timy set.seed(12) library("data.table") tabulka = data.frame(A=rep(c("C","D"),each=6),B=seq(10,43,by=3),C=rnorm(12),D=5:16) tabulka tabulka$B[c(2,5,11)]=NA sort(tabulka$C,decreasing = TRUE ) quantile(tabulka$D, c(0,0.15,0.56)) colSums( tabulka[,mean(B),!] library(base) CO2=CO2 CO2 par(mfrow = c(1,1)) boxplot(uptake~Treatment,CO2, col="blue",xlab="Treatment",ylab="uptake",main = "zavislost treatment id uptake") abline(h=28,lwd=5,lty=2,col ="red") library(lattice) xyplot(uptake~conc|Type,data= CO2) at Nov 6, 2017 base v3.4.1

a <-getwd() print(a)