Re: [R] Looking up the directory a file is located in

2010-11-18 Thread Barry Rowlingson
On Thu, Nov 18, 2010 at 12:00 AM, Cliff Clive cliffcl...@gmail.com wrote:

 Basically I'm just looking for a command that can look up the name of the
 directory of the script that is running.  If I move or copy the script to
 another directory, it should be able to read the name of the new directory
 without me having to edit the code.

 Once I have identified the directory, I can insert it into the setwd()
 command and continue with my program.

 This is a fairly-frequently asked question, and the questioner is
rarely satisfied with the answer.

 How is your script starting? Either:

 a) R is starting in that directory, so that the working directory is
already set, or

 b) Whatever runs the script knows where it is in order to start it,
so could pass that directory to the script somehow, either by a global
parameter, or an environment variable.

__
R-help@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.


Re: [R] Looking up the directory a file is located in

2010-11-18 Thread Cliff Clive

So it sounds like the best we can do in R is to keep track of the script in a
sort of master file that runs the script, and set the working directory in
the master.  Is that accurate?

In Python any time you run a script, there is a built-in __file__ variable
that can tell you the file name of the script itself.  It would be nice to
have a feature like this in R.
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Looking-up-the-directory-a-file-is-located-in-tp3047649p3048916.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Looking up the directory a file is located in

2010-11-18 Thread RICHARD M. HEIBERGER
On Thu, Nov 18, 2010 at 10:26 AM, Cliff Clive cliffcl...@gmail.com wrote:

 In Python any time you run a script, there is a built-in __file__
 variable
 that can tell you the file name of the script itself.  It would be nice to
 have a feature like this in R.

R has that feature.

source(c:/myfullpath/myfile.r, chdir=TRUE)

See ?source for details.

Rich

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Looking up the directory a file is located in

2010-11-18 Thread Barry Rowlingson
On Thu, Nov 18, 2010 at 3:26 PM, Cliff Clive cliffcl...@gmail.com wrote:

 So it sounds like the best we can do in R is to keep track of the script in a
 sort of master file that runs the script, and set the working directory in
 the master.  Is that accurate?

 Errr no. Maybe. What?

 Your script is somewhere. Something is calling it. The thing that
calls it knows where it is. The thing that calls it is then
responsible for telling the script where it is. For example:

here = C:/Fnord/
source(C:/Fnord/script.R)

 now the script in C:\Fnord\script.R looks at 'here' and finds its
data files by pasting 'data.txt' onto the value of 'here'.

This is of course still a fairly bad way of doing things. Better to
write functions that can live _anywhere_ and tell them where to find
their data. So instead of the above, you should end up with:

 source(C:/MyLibrary/mycode.R)
 nowProcess(C:/Fnord)

where nowProcess is a function that takes a directory to work on as
its first parameter.

But EVEN better still is to write a package. Then you do:

 library(myPackage)
 nowProcess(C:/Fnord)

 In Python any time you run a script, there is a built-in __file__ variable
 that can tell you the file name of the script itself.  It would be nice to
 have a feature like this in R.

 Indeed. Also, I'd like to get rid of curly brackets from R and use
indentation, and have a neater class mechanism... Perhaps decorators
for functions... Hmm...

Barry

__
R-help@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.


Re: [R] Looking up the directory a file is located in

2010-11-18 Thread Gabor Grothendieck
On Wed, Nov 17, 2010 at 4:08 PM, Cliff Clive cliffcl...@gmail.com wrote:

 Hello everyone,

 This should be an easy question, I think.

 I'd like to write a command in a program to set the working directory to
 whatever directory the file is currently stored in.  Suppose I have a file
 called myRscript.r, and it's stored in C:\Rprojects\myRscript.r, and it
 references other R scripts and data files in the same directory.

 If I enter the command

 setwd(C:/Rprojects)

 I can then access all the files I need without typing the path.

 But suppose I want to move all of those files into another folder, say,
 C:\NewFolder.  And suppose I might do this fairly often, or make copies of
 the script in several folders.  Is there a command that looks something like
 this:

 setwd( look up current directory )

 that will work no matter where I move my project, without having to go in
 and re-type the new directory path?

For two solutions see:

https://stat.ethz.ch/pipermail/r-help/2005-November/082347.html

http://www.mail-archive.com/r-help@r-project.org/msg111871.html



-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@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.


Re: [R] Looking up the directory a file is located in

2010-11-18 Thread Cliff Clive

Thanks, Gabor!  So far I like this one best:
https://stat.ethz.ch/pipermail/r-help/2005-November/082347.html

So if my script is called myRscript.r, I can do the following:


this.file = parent.frame(2)$ofile
this.dir = gsub(/myRscript.r, , this.file)
setwd(this.dir)


This will set the working directory to the the directory that myRscript.r
lives in, no matter where I move the script.  It's nice that it can be done
in only three lines of code, although it's not yet a perfect solution, since
it won't work if I change the name of the script.  But that's easy to take
care of if I just do some slightly more sophisticated string manipulation
(which I'm terrible at doing off the top of my head).
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Looking-up-the-directory-a-file-is-located-in-tp3047649p3049113.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Looking up the directory a file is located in

2010-11-18 Thread Duncan Murdoch

On 18/11/2010 11:56 AM, Cliff Clive wrote:

Thanks, Gabor!  So far I like this one best:
https://stat.ethz.ch/pipermail/r-help/2005-November/082347.html

So if my script is called myRscript.r, I can do the following:


this.file = parent.frame(2)$ofile
this.dir = gsub(/myRscript.r, , this.file)
setwd(this.dir)


This will set the working directory to the the directory that myRscript.r
lives in, no matter where I move the script.  It's nice that it can be done
in only three lines of code, although it's not yet a perfect solution, since
it won't work if I change the name of the script.  But that's easy to take
care of if I just do some slightly more sophisticated string manipulation
(which I'm terrible at doing off the top of my head).


You can use the dirname() function to remove the filename from the path, 
you don't need to know the filename.  But you are making some strong 
assumptions in this code that may not hold:


 - that your script was invoked by source(), rather than piping into R, 
or using Rscript, or R CMD BATCH...
 - that source() has a local variable named ofile (which it does, but 
it might not in the future)


I would say it's bad practice for your script to change directories and 
not restore the old one, but that's a matter of taste.


I think the other solutions you were offered are better than this one.

Duncan Murdoch

__
R-help@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.


Re: [R] Looking up the directory a file is located in

2010-11-18 Thread Cliff Clive

The dirname() function looks very helpful; I hadn't heard of that one before.

I'm still reading up to see how the parent.frame command does what it does;
I didn't realize that the ofile variable might not be around in the future.  

Richard's suggestion -- source(c:/myfullpath/myfile.r, chdir=TRUE) -- is
probably the best; I would still have to update myfullpath if I move
myfile.r, but that's not hard to do.

-- 
View this message in context: 
http://r.789695.n4.nabble.com/Looking-up-the-directory-a-file-is-located-in-tp3047649p3049394.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Looking up the directory a file is located in

2010-11-17 Thread jim holtman
What do you expect it to look up?  How do you want to specify the
directory?  Is it supposed to search through some sequence or use ESP?

On Wed, Nov 17, 2010 at 4:08 PM, Cliff Clive cliffcl...@gmail.com wrote:

 Hello everyone,

 This should be an easy question, I think.

 I'd like to write a command in a program to set the working directory to
 whatever directory the file is currently stored in.  Suppose I have a file
 called myRscript.r, and it's stored in C:\Rprojects\myRscript.r, and it
 references other R scripts and data files in the same directory.

 If I enter the command

 setwd(C:/Rprojects)

 I can then access all the files I need without typing the path.

 But suppose I want to move all of those files into another folder, say,
 C:\NewFolder.  And suppose I might do this fairly often, or make copies of
 the script in several folders.  Is there a command that looks something like
 this:

 setwd( look up current directory )

 that will work no matter where I move my project, without having to go in
 and re-type the new directory path?


 Thanks in advance,

 Cliff


 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Looking-up-the-directory-a-file-is-located-in-tp3047649p3047649.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@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.




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
R-help@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.


Re: [R] Looking up the directory a file is located in

2010-11-17 Thread Cliff Clive

Basically I'm just looking for a command that can look up the name of the
directory of the script that is running.  If I move or copy the script to
another directory, it should be able to read the name of the new directory
without me having to edit the code.

Once I have identified the directory, I can insert it into the setwd()
command and continue with my program.
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Looking-up-the-directory-a-file-is-located-in-tp3047649p3047878.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.