setting your working directory permanently in R

Most of us R users are using a special working directory for the daily work in R. But I was bothered in typing everytime


in my command line prior using R. Also using this line at the first position in scripts was not pleasent enough.
So how to get around this? There is a special file in R for adjusting your startup-parameters. It is called “”. In a linux environment you may type:

sudo gedit /etc/R/

In a Windows OS you just need to search for the file mentioned above and open it up in an editor of your choice.
You will be confronted with the following lines:

##						Emacs please make this -*- R -*-
## empty for R on Debian
## Copyright (C) 2008 Dirk Eddelbuettel and GPL'ed
## see help(Startup) for documentation on ~/.Rprofile and

# ## Example of .Rprofile
# options(width=65, digits=5)
# options(show.signif.stars=FALSE)
# setHook(packageEvent("grDevices", "onLoad"),
#         function(...) grDevices::ps.options(horizontal=FALSE))
# set.seed(1234)
 .First <- function() cat("n   Welcome to R!nn")
 .Last <- function()  cat("n   Goodbye!nn")

# ## Example of
# local({
#  # add MASS to the default packages, set a CRAN mirror
#  old <- getOption("defaultPackages"); r <- getOption("repos")
#  r["CRAN"] <- "http://my.local.cran"
#  options(defaultPackages = c(old, "MASS"), repos = r)

Between the lines 14 and 15 you can set your start-up parameters. So by filling in


the startup directory will be set permanently to your desired working directory. Of course you can also add lines like

library("sp", "rgdal")

to increase your workflow. ENJOY!

5 1 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments

[…] your default working directory in Rstudio as well as it differs from the one you had set using setwd(“path”): default path setting in Rstudio (click to enlarge) So lets come to the *.Rnw […]

11 years ago

Exactly what I was looking for, thank you.

S├ębastien Boillat
S├ębastien Boillat
8 years ago

my files does not look like that (using Windows 7 64bits, R 3.2.1), I have a single line with other parameters. Where should I enter the setwd command? Furthermore, I cannot save my file into a .site file using the text editor, only .txt

Giovanni Giappo Laudanno
Giovanni Giappo Laudanno
7 years ago

Is there any command on R to get the address of the standard wd (not to set it)?

7 years ago

[here]( or in short: getwd()

Giovanni Giappo Laudanno
Giovanni Giappo Laudanno
7 years ago
Reply to  Riccardo

Don’t you think that if it was so easy I would have probably solve it myself?
If you don’t know the solution even an “I honestly don’t know” could be a decent answer…

7 years ago

what do you mean by “standard wd”?

Giovanni Giappo Laudanno
Giovanni Giappo Laudanno
7 years ago
Reply to  Riccardo

Standard wd is not the current wd (that’s what you get with getwd().
It is what you can set on Rstudio with Tools>Global Options>Default working directory.
Before you ask: I cannot copy paste from there because I am working on different computers with different default working directories for each of them.