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

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jun 13 15:07:30 CEST 2010


On Thu, Jun 10, 2010 at 8:33 PM, 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())
>

This variation is nearly the same:

https://stat.ethz.ch/pipermail/r-help/2010-May/238804.html



More information about the R-help mailing list