Path to R script

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Path to R script

Johannes W. Dietrich
There is certainly a trivial solution for my question, but I can't
find the answer in the documentation.

I need a platform independent method to obtain the file path of the
current R script.

My working group uses R on several machines with different operating
systems including Mac OS X, Windows and Linux, and for obvious
reasons the file hierarchies are very different. Therefore, it would
be useful to be able to address data files and external procedures
relative to the path of the script.

Thank you for any suggestion,

J. W. D.
--
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- Dr. Johannes W. Dietrich, M.D.
-- Laboratory XU 44, Endocrine Research
-- Medical Hospital I, Bergmannsheil University Hospitals
-- Ruhr University of Bochum
-- Buerkle-de-la-Camp-Platz 1, D-44789 Bochum, NRW, Germany
-- Phone: +49:234:302-6400, Fax: +49:234:302-6403
-- eMail: "[hidden email]"
-- WWW: http://medical-cybernetics.de
-- WWW: http://www.bergmannsheil.de
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

______________________________________________
[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: Path to R script

Erik Iverson-3
While not a direct answer to your question, do

?getwd and ?setwd help at all?

Johannes W. Dietrich wrote:

> There is certainly a trivial solution for my question, but I can't find
> the answer in the documentation.
>
> I need a platform independent method to obtain the file path of the
> current R script.
>
> My working group uses R on several machines with different operating
> systems including Mac OS X, Windows and Linux, and for obvious reasons
> the file hierarchies are very different. Therefore, it would be useful
> to be able to address data files and external procedures relative to the
> path of the script.
>
> Thank you for any suggestion,
>
> J. W. D.

______________________________________________
[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: Path to R script

Duncan Murdoch-2
In reply to this post by Johannes W. Dietrich
On 12/05/2010 3:31 PM, Johannes W. Dietrich wrote:

> There is certainly a trivial solution for my question, but I can't
> find the answer in the documentation.
>
> I need a platform independent method to obtain the file path of the
> current R script.
>
> My working group uses R on several machines with different operating
> systems including Mac OS X, Windows and Linux, and for obvious
> reasons the file hierarchies are very different. Therefore, it would
> be useful to be able to address data files and external procedures
> relative to the path of the script.

I'm not sure there's a trivial solution, because there are so many
different ways to run a script.  How are you running it?  Are you using
the same method on all platforms?

Duncan Murdoch

______________________________________________
[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: Path to R script

Shi, Tao
In reply to this post by Johannes W. Dietrich
Don't quite understand your question, but it looks like a more IT issue to me.  I guess you store your R scripts in a central location (e.g. a server) and everybody call them from their own workstation, right?  Or you can always bundle your R scripts into a package and distribute it around your group.

...Tao




----- Original Message ----
> From: Johannes W. Dietrich <[hidden email]>
> To: [hidden email]
> Sent: Wed, May 12, 2010 12:31:19 PM
> Subject: [R] Path to R script
>
> There is certainly a trivial solution for my question, but I can't find the
> answer in the documentation.

I need a platform independent method to
> obtain the file path of the current R script.

My working group uses R on
> several machines with different operating systems including Mac OS X, Windows
> and Linux, and for obvious reasons the file hierarchies are very different.
> Therefore, it would be useful to be able to address data files and external
> procedures relative to the path of the script.

Thank you for any
> suggestion,

J. W. D.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> -- -- -- -- -- -- -- -- -- --
-- Dr. Johannes W. Dietrich, M.D.
--
> Laboratory XU 44, Endocrine Research
-- Medical Hospital I, Bergmannsheil
> University Hospitals
-- Ruhr University of Bochum
--
> Buerkle-de-la-Camp-Platz 1, D-44789 Bochum, NRW, Germany
-- Phone:
> +49:234:302-6400, Fax: +49:234:302-6403
-- eMail: "
> ymailto="mailto:[hidden email]"
> href="mailto:[hidden email]">[hidden email]"
--
> WWW: http://medical-cybernetics.de
-- WWW: http://www.bergmannsheil.de
--
> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> --

______________________________________________

> ymailto="mailto:[hidden email]"
> href="mailto:[hidden email]">[hidden email] mailing list

> href="https://stat.ethz.ch/mailman/listinfo/r-help" target=_blank
> >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: Path to R script

Sharpie
In reply to this post by Johannes W. Dietrich
Johannes W. Dietrich wrote
There is certainly a trivial solution for my question, but I can't
find the answer in the documentation.

I need a platform independent method to obtain the file path of the
current R script.

My working group uses R on several machines with different operating
systems including Mac OS X, Windows and Linux, and for obvious
reasons the file hierarchies are very different. Therefore, it would
be useful to be able to address data files and external procedures
relative to the path of the script.

Thank you for any suggestion,

J. W. D.
--
The best recipie I have for dealing with this situation is:

  1. Place R scripts in a package (a very good idea anyway if you are distributing code to others).

  2. Place a configuration file in the package that contains info such as paths to data directories.

  3. Customize the configuration file after installing the package (if necessary).

Then your script accesses the configuration file, which is in a known fixed location, and uses the information inside to find other files that may be in unknown and changing locations.

You could also place the configuration file in a location such as:

~/.MyPackageConf

Since:

  path.expand( '~/' )

Has a pretty predictable behavior across platforms.  This would save it from being overwritten every time you updated the package.

Hope this helps!

-Charlie
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
Reply | Threaded
Open this post in threaded view
|

Re: Path to R script

David Scott-6
Sharpie wrote:

>
> Johannes W. Dietrich wrote:
>> There is certainly a trivial solution for my question, but I can't
>> find the answer in the documentation.
>>
>> I need a platform independent method to obtain the file path of the
>> current R script.
>>
>> My working group uses R on several machines with different operating
>> systems including Mac OS X, Windows and Linux, and for obvious
>> reasons the file hierarchies are very different. Therefore, it would
>> be useful to be able to address data files and external procedures
>> relative to the path of the script.
>>
>> Thank you for any suggestion,
>>
>> J. W. D.
>> --
>>
>
> The best recipie I have for dealing with this situation is:
>
>   1. Place R scripts in a package (a very good idea anyway if you are
> distributing code to others).
>
>   2. Place a configuration file in the package that contains info such as
> paths to data directories.
>
>   3. Customize the configuration file after installing the package (if
> necessary).
>
> Then your script accesses the configuration file, which is in a known fixed
> location, and uses the information inside to find other files that may be in
> unknown and changing locations.
>
> You could also place the configuration file in a location such as:
>
> ~/.MyPackageConf
>
> Since:
>
>   path.expand( '~/' )
>
> Has a pretty predictable behavior across platforms.  This would save it from
> being overwritten every time you updated the package.
>
> Hope this helps!
>
> -Charlie

I also was about to suggest putting the code in a package because then
you have system.file available, which I presume works across platforms:

 > system.file("RunTests.R",package="DistributionUtils")
[1] "/usr/local/david/R/DistributionUtils"
 > system.file("unitTests/runTests.R",package="DistributionUtils")
[1] "/usr/local/david/R/DistributionUtils/unitTests/runTests.R"


David Scott


_________________________________________________________________
David Scott Department of Statistics
                The University of Auckland, PB 92019
                Auckland 1142,    NEW ZEALAND
Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055
Email: [hidden email],  Fax: +64 9 373 7018

Director of Consulting, Department of Statistics

______________________________________________
[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: Path to R script

David Scott-6
David Scott wrote:

> Sharpie wrote:
>> Johannes W. Dietrich wrote:
>>> There is certainly a trivial solution for my question, but I can't
>>> find the answer in the documentation.
>>>
>>> I need a platform independent method to obtain the file path of the
>>> current R script.
>>>
>>> My working group uses R on several machines with different operating
>>> systems including Mac OS X, Windows and Linux, and for obvious
>>> reasons the file hierarchies are very different. Therefore, it would
>>> be useful to be able to address data files and external procedures
>>> relative to the path of the script.
>>>
>>> Thank you for any suggestion,
>>>
>>> J. W. D.
>>> --
>>>
>> The best recipie I have for dealing with this situation is:
>>
>>   1. Place R scripts in a package (a very good idea anyway if you are
>> distributing code to others).
>>
>>   2. Place a configuration file in the package that contains info such as
>> paths to data directories.
>>
>>   3. Customize the configuration file after installing the package (if
>> necessary).
>>
>> Then your script accesses the configuration file, which is in a known fixed
>> location, and uses the information inside to find other files that may be in
>> unknown and changing locations.
>>
>> You could also place the configuration file in a location such as:
>>
>> ~/.MyPackageConf
>>
>> Since:
>>
>>   path.expand( '~/' )
>>
>> Has a pretty predictable behavior across platforms.  This would save it from
>> being overwritten every time you updated the package.
>>
>> Hope this helps!
>>
>> -Charlie
>
> I also was about to suggest putting the code in a package because then
> you have system.file available, which I presume works across platforms:
>
>  > system.file("RunTests.R",package="DistributionUtils")
> [1] "/usr/local/david/R/DistributionUtils"
>  > system.file("unitTests/runTests.R",package="DistributionUtils")
> [1] "/usr/local/david/R/DistributionUtils/unitTests/runTests.R"


Sorry, I copied that incorrectly. Should be:

 > system.file(package="DistributionUtils")
[1] "/usr/local/david/R/DistributionUtils"
 > system.file("unitTests/runTests.R",package="DistributionUtils")
[1] "/usr/local/david/R/DistributionUtils/unitTests/runTests.R"

David Scott
_________________________________________________________________
David Scott Department of Statistics
                The University of Auckland, PB 92019
                Auckland 1142,    NEW ZEALAND
Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055
Email: [hidden email],  Fax: +64 9 373 7018

Director of Consulting, Department of Statistics

______________________________________________
[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: Path to R script

Gabor Grothendieck
In reply to this post by Johannes W. Dietrich
If you are running the file via source("myfile.R") then just put this
in the file:

this.dir <- dirname(sys.frame(1)$ofile)

This is a bit fragile since changes to the internals of source could
break it but it does currently work.

On Wed, May 12, 2010 at 3:31 PM, Johannes W. Dietrich
<[hidden email]> wrote:

> There is certainly a trivial solution for my question, but I can't find the
> answer in the documentation.
>
> I need a platform independent method to obtain the file path of the current
> R script.
>
> My working group uses R on several machines with different operating systems
> including Mac OS X, Windows and Linux, and for obvious reasons the file
> hierarchies are very different. Therefore, it would be useful to be able to
> address data files and external procedures relative to the path of the
> script.
>
> Thank you for any suggestion,
>
> J. W. D.
> --
> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> -- Dr. Johannes W. Dietrich, M.D.
> -- Laboratory XU 44, Endocrine Research
> -- Medical Hospital I, Bergmannsheil University Hospitals
> -- Ruhr University of Bochum
> -- Buerkle-de-la-Camp-Platz 1, D-44789 Bochum, NRW, Germany
> -- Phone: +49:234:302-6400, Fax: +49:234:302-6403
> -- eMail: "[hidden email]"
> -- WWW: http://medical-cybernetics.de
> -- WWW: http://www.bergmannsheil.de
> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>
> ______________________________________________
> [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: Path to R script

Johannes W. Dietrich
In reply to this post by Shi, Tao
At 13:19 Uhr -0700 12.05.2010, Shi, Tao wrote:
>Don't quite understand your question, but it looks like a more IT
>issue to me.  I guess you store your R scripts in a central location
>(e.g. a server) and everybody call them from their own workstation,
>right?

Thank you, but this would be an easy solution. The problem is that
the R script and data tables are stored on the individual computers
(and therefore on different places depending of the users'
preferences and of course the operating system).

If it were possible to locate the files relative to the script we
could write modular scripts that would run on any machine without
modifications.

Thanks again,

J. W. D.
--
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- Dr. Johannes W. Dietrich, M.D.
-- Laboratory XU 44, Endocrine Research
-- Medical Hospital I, Bergmannsheil University Hospitals
-- Ruhr University of Bochum
-- Buerkle-de-la-Camp-Platz 1, D-44789 Bochum, NRW, Germany
-- Phone: +49:234:302-6400, Fax: +49:234:302-6403
-- eMail: "[hidden email]"
-- WWW: http://medical-cybernetics.de
-- WWW: http://www.bergmannsheil.de
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

______________________________________________
[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: Path to R script

Johannes W. Dietrich
In reply to this post by Gabor Grothendieck
At 19:29 Uhr -0400 12.05.2010, Gabor Grothendieck wrote:
>If you are running the file via source("myfile.R") then just put this
>in the file:
>
>this.dir <- dirname(sys.frame(1)$ofile)
>
>This is a bit fragile since changes to the internals of source could
>break it but it does currently work.

Great, this is the solution. It works if the script has been saved
before running.

Thank you,

J. W. D.
--
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- Dr. Johannes W. Dietrich, M.D.
-- Laboratory XU 44, Endocrine Research
-- Medical Hospital I, Bergmannsheil University Hospitals
-- Ruhr University of Bochum
-- Buerkle-de-la-Camp-Platz 1, D-44789 Bochum, NRW, Germany
-- Phone: +49:234:302-6400, Fax: +49:234:302-6403
-- eMail: "[hidden email]"
-- WWW: http://medical-cybernetics.de
-- WWW: http://www.bergmannsheil.de
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

______________________________________________
[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: Path to R script

Shi, Tao
In reply to this post by Johannes W. Dietrich
In this case, deploy them as a R package is the way to go.




----- Original Message ----

> From: Johannes W. Dietrich <[hidden email]>
> To: "Shi, Tao" <[hidden email]>; [hidden email]
> Cc: [hidden email]
> Sent: Thu, May 13, 2010 9:14:24 AM
> Subject: Re: [R] Path to R script
>
> At 13:19 Uhr -0700 12.05.2010, Shi, Tao wrote:
> Don't quite understand
> your question, but it looks like a more IT issue to me.  I guess you store
> your R scripts in a central location (e.g. a server) and everybody call them
> from their own workstation, right?

Thank you, but this would be an easy
> solution. The problem is that the R script and data tables are stored on the
> individual computers (and therefore on different places depending of the users'
> preferences and of course the operating system).

If it were possible to
> locate the files relative to the script we could write modular scripts that
> would run on any machine without modifications.

Thanks again,

J.
> W. D.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> -- --
-- Dr. Johannes W. Dietrich, M.D.
-- Laboratory XU 44, Endocrine
> Research
-- Medical Hospital I, Bergmannsheil University Hospitals
-- Ruhr
> University of Bochum
-- Buerkle-de-la-Camp-Platz 1, D-44789 Bochum, NRW,
> Germany
-- Phone: +49:234:302-6400, Fax: +49:234:302-6403
-- eMail: "
> ymailto="mailto:[hidden email]"
> href="mailto:[hidden email]">[hidden email]"
--
> WWW: http://medical-cybernetics.de
-- WWW: http://www.bergmannsheil.de
--
> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

______________________________________________
[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: Path to R script

Sharpie
In reply to this post by Johannes W. Dietrich
Johannes W. Dietrich wrote
At 19:29 Uhr -0400 12.05.2010, Gabor Grothendieck wrote:
>If you are running the file via source("myfile.R") then just put this
>in the file:
>
>this.dir <- dirname(sys.frame(1)$ofile)
>
>This is a bit fragile since changes to the internals of source could
>break it but it does currently work.

Great, this is the solution. It works if the script has been saved
before running.

Thank you,

J. W. D.
I *strongly* suggest you consider packages if you are sharing collections of R functions and scripts with other people.  Compared to other programming languages I have used, R makes it dead simple to create packages.  All you really need to get started is:

  ?package.skeleton()

That command creates a set of folders whose structure is pretty self explanatory:

  package.skeleton( 'myPackage' )

Creates the beginnings of a package structure:

  myPackage/
   
     R/  => Files containing your custom R functions go in here

     data/  => Data sets go in here

     man/  => For documentation files

     inst/   => Everything else such as scripts and config files

The "Writing R Extensions Manual" can help you fill in the rest of the blanks.

The bottom line is that creating a package cost a minimal investment of extra time and pays back huge dividends if the collection of scripts and data files you are sharing gets complex or you people ask you to document the software that they are starting to depend on.

-Charlie
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University