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

Henrik Bengtsson hb at stat.berkeley.edu
Fri Jun 11 11:30:46 CEST 2010


Isn't this what source(..., chdir=TRUE) is for?  See help(source).

/H

On Fri, Jun 11, 2010 at 2:33 AM, Marcin Gomulka <mrgomel at gmail.com> 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?
>
> Thanks,
> mrgomel
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list