setting the current working directory to the location of the source file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

setting the current working directory to the location of the source file

Marcin Gomulka
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

mohan L
On Fri, Jun 11, 2010 at 6:03 AM, Marcin Gomulka <[hidden email]> 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?
>
>
Hi,

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

##set working directory

pwd <- getwd()
if (!is.null(pwd)) {
                setwd(pwd)
        } else {
                print("Directory not found")
        }

Thanks & Rg
Mohan L

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

cberry
In reply to this post by Marcin Gomulka
On Fri, 11 Jun 2010, Marcin Gomulka wrote:

> AFAIK a script run through source() does not have any legit way to learn
> about it's own location.
>

You mean like 'test.R' being able to figure out that that is what it is
called when it is invoked from source()?


> cat("print(eval(sys.calls()[[1]][[2]]))",file='test.R')
> source('test.R')
[1] "test.R"
> setwd('projects')
> source('../test.R')
[1] "../test.R"
> source(list.files('..','test.R$',full=TRUE))
[1] "../test.R"
>
>

See

  ?sys.calls


HTH,

Chuck


> 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]]
>
> ______________________________________________
> [hidden email] 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:[hidden email]            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

Henrik Bengtsson
In reply to this post by Marcin Gomulka
Isn't this what source(..., chdir=TRUE) is for?  See help(source).

/H

On Fri, Jun 11, 2010 at 2:33 AM, Marcin Gomulka <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

Marcin Gomulka
In reply to this post by cberry
Charles' hint was what I was looking for. Thanks!

mg.



On Fri, Jun 11, 2010 at 7:40 AM, Charles C. Berry <[hidden email]>wrote:

>
>
>  cat("print(eval(sys.calls()[[1]][[2]]))",file='test.R')
>>
>
>>
>>
> See
>
>        ?sys.calls
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

Marcin Gomulka
In reply to this post by Henrik Bengtsson
On Fri, Jun 11, 2010 at 11:30 AM, Henrik Bengtsson <[hidden email]>wrote:

> Isn't this what source(..., chdir=TRUE) is for?  See help(source).
>
>
not really. Imagine you give someone a script, but you have no control over
where and how they run it. They shouldn't be required to put in the chdir
parameter.
Say, the script outputs a plot to a file (pdf,png,...) and you want the file
to be created next to the source script, as the user would expect it.

mg.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

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

Gabor Grothendieck
In reply to this post by Marcin Gomulka
On Thu, Jun 10, 2010 at 8:33 PM, Marcin Gomulka <[hidden email]> 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

______________________________________________
[hidden email] 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.