setting your working directory permanently in R

[This article was first published on geo-affine » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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!\n\n")
 .Last <- function()  cat("\n   Goodbye!\n\n")

# ## 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!

To leave a comment for the author, please follow the link and comment on their blog: geo-affine » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)