Digital Geography

24. June 2012

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!