Re: [R] setting the current working directory to the location of the source file

From: Mohan L <>
Date: Fri, 11 Jun 2010 09:56:43 +0530

On Fri, Jun 11, 2010 at 6:03 AM, Marcin Gomulka <> wrote:

> AFAIK a script run through source() does not have any legit way to learn
> about it's own location.
> I need this to make sure that the script will find its datafiles after I
> move the whole directory. (The datafiles are in the same directory.)
> Here is a hack I invented to work around it:
> print(getwd())
> source_pathname = get("ofile",envir = parent.frame())
> source_dirname = dirname(source_pathname )
> setwd(source_dirname)
> print(getwd())
> Question: Is there a better, cleaner way?

I am using some thing like this to setup working directory:

##set working directory

pwd <- getwd()
if (!is.null(pwd)) {

        } else {
                print("Directory not found")

Thanks & Rg
Mohan L

        [[alternative HTML version deleted]] mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Fri 11 Jun 2010 - 04:29:09 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 Jun 2010 - 04:30:29 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive