Digital Geography

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!

8
Leave a Reply

avatar
4 Comment threads
4 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
RiccardoGiovanni Giappo LaudannoSébastien Boillatdafedareproducible documents/analytics in R: the knitr package « Data Management « geo-affine Recent comment authors

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

  Subscribe  
newest oldest most voted
Notify of
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
Guest
dafeda

Exactly what I was looking for, thank you.

Sébastien Boillat
Guest
Sébastien Boillat

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
Guest
Giovanni Giappo Laudanno

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

Riccardo
Guest

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

Giovanni Giappo Laudanno
Guest
Giovanni Giappo Laudanno

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
Guest

what do you mean by “standard wd”?

Giovanni Giappo Laudanno
Guest
Giovanni Giappo Laudanno

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.