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

setwd("/home/rikl/wd_r")

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 “Rprofile.site”. In a linux environment you may type:

sudo gedit /etc/R/Rprofile.site

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 Rprofile.site for R on Debian
##
## Copyright (C) 2008 Dirk Eddelbuettel and GPL'ed
##
## see help(Startup) for documentation on ~/.Rprofile and Rprofile.site

# ## 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 Rprofile.site
# 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

setwd("/home/rikl/wd_r")

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
Subscribe
Notify of
guest

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

8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] 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 […]

dafeda
dafeda
13 years ago

Exactly what I was looking for, thank you.

Sébastien Boillat
Sébastien Boillat
11 years ago

my RProfile.site 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
10 years ago

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

Riccardo
10 years ago

[here](http://lmgtfy.com/?q=R+get+working+directory) or in short: getwd()

Giovanni Giappo Laudanno
Giovanni Giappo Laudanno
10 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…

Riccardo
10 years ago

what do you mean by “standard wd”?

Giovanni Giappo Laudanno
Giovanni Giappo Laudanno
10 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.