Re: [R] How to find the path or the current file?

2009-03-25 Thread Wacek Kusnierczyk

hacking up on gabor's solution, i've created a trivial function that
will allow you to access a file given a path relative to the path of the
file calling the function.

to be concrete, suppose you have two files -- one library and one
executable -- located in two sibling directories, and you want one of
them to access (e.g., source) the other without the need to specify the
absolute path, and irrespectively of the current working directory. 
here is a simple example.

mkdir foo/{bin,lib} -p
   
echo '
   # the library file
   foo = function() cat(foo\n)
'  foo/lib/lib.r

echo '
   # the executable file
   source(http://miscell.googlecode.com/svn/rpath/rpath.r;)
   source(rpath(../lib/lib.r))
   foo()
'  foo/bin/bin.r

now you can execute foo/bin/bin.r from whatever location, or source it
in r within whatever working directory, and still have it load
foo/lib/lib.r:

r foo/bin/bin.r
# foo

(cd foo; r bin/bin.r)
# foo

r -e 'source(foo/bin/bin.r)'
# foo

(cd foo/bin; r -e 'source(bin.r)')
# foo

so the trick for you is to source rpath, and voila.  (note, it's not
foolproof;  as duncan explained, such approach may not work in some
circumstances.)

does this address your problem?

hilsen,
vQ

Gabor Grothendieck wrote:
 See:

 https://stat.ethz.ch/pipermail/r-help/2009-January/184745.html

 On Tue, Mar 24, 2009 at 7:16 AM, Marie Sivertsen mariesiv...@gmail.com 
 wrote:
   
 Dear useRs,

 I have a collection of source file and some of these call others.  The files
 are distribute among a number of directories, and to know how to call some
 other file they need to know what file is currently executed.

 As example, I have a file 'search.R' located in directory 'bin' which needs
 to access the file 'terms.conf' located in the directory 'conf', a sibling
 of 'bin'.  I can have somethings like readLines('../conf/terms.conf') in
 search.R, but this work only if search.R is executed from bin, when getwd is
 'bin'.  But when search.R calls from the parent as bin/search.R or any other
 derectory then R complains that it could not find the file
 '../conf/terms.conf'.

 So my questions is:  how can the file search.R, when executied, discover its
 own location and load terms.conf from location of
 search.R/../conf/terms.conf?  the location of search.R can be unrelated to
 the current directory.

 Mvh.
 Marie

__
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] How to find the path or the current file?

2009-03-25 Thread Wacek Kusnierczyk
Wacek Kusnierczyk wrote:
 hacking up on gabor's solution, i've created a trivial function that
 will allow you to access a file given a path relative to the path of the
 file calling the function.

 to be concrete, suppose you have two files -- one library and one
 executable -- located in two sibling directories, and you want one of
 them to access (e.g., source) the other without the need to specify the
 absolute path, and irrespectively of the current working directory. 
 here is a simple example.

 mkdir foo/{bin,lib} -p

 echo '
# the library file
foo = function() cat(foo\n)
 '  foo/lib/lib.r

 echo '
# the executable file
source(http://miscell.googlecode.com/svn/rpath/rpath.r;)
source(rpath(../lib/lib.r))
foo()
 '  foo/bin/bin.r
   

one thing i forgot to add:  that contrarily to what gabor warned about
his solution, you don't have to have the call to rpath at the top level,
and can embed it in nested nevironments or calls; thus, the following
executable:

echo '
   # the executable file
   source(http://miscell.googlecode.com/svn/rpath/rpath.r;)
   (function()
  (function()
 (function() {
source(rpath(../lib/lib.r))
foo() })())())()
'  foo/bin/bin.r

will still work as below.

 now you can execute foo/bin/bin.r from whatever location, or source it
 in r within whatever working directory, and still have it load
 foo/lib/lib.r:

 r foo/bin/bin.r
 # foo

 (cd foo; r bin/bin.r)
 # foo

 r -e 'source(foo/bin/bin.r)'
 # foo

 (cd foo/bin; r -e 'source(bin.r)')
 # foo

 so the trick for you is to source rpath, and voila.  (note, it's not
 foolproof;  as duncan explained, such approach may not work in some
 circumstances.)

 does this address your problem?

 hilsen,
 vQ

 Gabor Grothendieck wrote:
   
 See:

 https://stat.ethz.ch/pipermail/r-help/2009-January/184745.html

 On Tue, Mar 24, 2009 at 7:16 AM, Marie Sivertsen mariesiv...@gmail.com 
 wrote:
   
 
 Dear useRs,

 I have a collection of source file and some of these call others.  The files
 are distribute among a number of directories, and to know how to call some
 other file they need to know what file is currently executed.

 As example, I have a file 'search.R' located in directory 'bin' which needs
 to access the file 'terms.conf' located in the directory 'conf', a sibling
 of 'bin'.  I can have somethings like readLines('../conf/terms.conf') in
 search.R, but this work only if search.R is executed from bin, when getwd is
 'bin'.  But when search.R calls from the parent as bin/search.R or any other
 derectory then R complains that it could not find the file
 '../conf/terms.conf'.

 So my questions is:  how can the file search.R, when executied, discover its
 own location and load terms.conf from location of
 search.R/../conf/terms.conf?  the location of search.R can be unrelated to
 the current directory.

 Mvh.
 Marie

__
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] How to find the path or the current file?

2009-03-25 Thread Marie Sivertsen
Wacek, this work for me.  Takk!

Mvh.
Marie

On Wed, Mar 25, 2009 at 2:59 PM, Wacek Kusnierczyk 
waclaw.marcin.kusnierc...@idi.ntnu.no wrote:

 Wacek Kusnierczyk wrote:
  hacking up on gabor's solution, i've created a trivial function that
  will allow you to access a file given a path relative to the path of the
  file calling the function.
 
  to be concrete, suppose you have two files -- one library and one
  executable -- located in two sibling directories, and you want one of
  them to access (e.g., source) the other without the need to specify the
  absolute path, and irrespectively of the current working directory.
  here is a simple example.
 
  mkdir foo/{bin,lib} -p
 
  echo '
 # the library file
 foo = function() cat(foo\n)
  '  foo/lib/lib.r
 
  echo '
 # the executable file
 source(http://miscell.googlecode.com/svn/rpath/rpath.r;)
 source(rpath(../lib/lib.r))
 foo()
  '  foo/bin/bin.r
 

 one thing i forgot to add:  that contrarily to what gabor warned about
 his solution, you don't have to have the call to rpath at the top level,
 and can embed it in nested nevironments or calls; thus, the following
 executable:

echo '
   # the executable file
   source(http://miscell.googlecode.com/svn/rpath/rpath.r;)
(function()
  (function()
 (function() {
 source(rpath(../lib/lib.r))
foo() })())())()
'  foo/bin/bin.r

 will still work as below.

  now you can execute foo/bin/bin.r from whatever location, or source it
  in r within whatever working directory, and still have it load
  foo/lib/lib.r:
 
  r foo/bin/bin.r
  # foo
 
  (cd foo; r bin/bin.r)
  # foo
 
  r -e 'source(foo/bin/bin.r)'
  # foo
 
  (cd foo/bin; r -e 'source(bin.r)')
  # foo
 
  so the trick for you is to source rpath, and voila.  (note, it's not
  foolproof;  as duncan explained, such approach may not work in some
  circumstances.)
 
  does this address your problem?
 
  hilsen,
  vQ
 
  Gabor Grothendieck wrote:
 
  See:
 
  https://stat.ethz.ch/pipermail/r-help/2009-January/184745.html
 
  On Tue, Mar 24, 2009 at 7:16 AM, Marie Sivertsen mariesiv...@gmail.com
 wrote:
 
 
  Dear useRs,
 
  I have a collection of source file and some of these call others.  The
 files
  are distribute among a number of directories, and to know how to call
 some
  other file they need to know what file is currently executed.
 
  As example, I have a file 'search.R' located in directory 'bin' which
 needs
  to access the file 'terms.conf' located in the directory 'conf', a
 sibling
  of 'bin'.  I can have somethings like readLines('../conf/terms.conf')
 in
  search.R, but this work only if search.R is executed from bin, when
 getwd is
  'bin'.  But when search.R calls from the parent as bin/search.R or any
 other
  derectory then R complains that it could not find the file
  '../conf/terms.conf'.
 
  So my questions is:  how can the file search.R, when executied,
 discover its
  own location and load terms.conf from location of
  search.R/../conf/terms.conf?  the location of search.R can be
 unrelated to
  the current directory.
 
  Mvh.
  Marie



[[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.


[R] How to find the path or the current file?

2009-03-24 Thread Marie Sivertsen
Dear useRs,

I have a collection of source file and some of these call others.  The files
are distribute among a number of directories, and to know how to call some
other file they need to know what file is currently executed.

As example, I have a file 'search.R' located in directory 'bin' which needs
to access the file 'terms.conf' located in the directory 'conf', a sibling
of 'bin'.  I can have somethings like readLines('../conf/terms.conf') in
search.R, but this work only if search.R is executed from bin, when getwd is
'bin'.  But when search.R calls from the parent as bin/search.R or any other
derectory then R complains that it could not find the file
'../conf/terms.conf'.

So my questions is:  how can the file search.R, when executied, discover its
own location and load terms.conf from location of
search.R/../conf/terms.conf?  the location of search.R can be unrelated to
the current directory.

Mvh.
Marie

[[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] How to find the path or the current file?

2009-03-24 Thread Duncan Murdoch

On 24/03/2009 7:16 AM, Marie Sivertsen wrote:

Dear useRs,

I have a collection of source file and some of these call others.  The files
are distribute among a number of directories, and to know how to call some
other file they need to know what file is currently executed.

As example, I have a file 'search.R' located in directory 'bin' which needs
to access the file 'terms.conf' located in the directory 'conf', a sibling
of 'bin'.  I can have somethings like readLines('../conf/terms.conf') in
search.R, but this work only if search.R is executed from bin, when getwd is
'bin'.  But when search.R calls from the parent as bin/search.R or any other
derectory then R complains that it could not find the file
'../conf/terms.conf'.

So my questions is:  how can the file search.R, when executied, discover its
own location and load terms.conf from location of
search.R/../conf/terms.conf?  the location of search.R can be unrelated to
the current directory.


In general it can't.  Since source() can work on a connection and a 
connection doesn't have to be a file, there may not be a location.


You could write your own Source function, something like this:

filenamestack - c()

Source - function(filename, ...) {
   # push the new filename
   filenamestack - c(filename, filenamestack)

   # on exit pop it off the stack
   on.exit(filenamestack - filenamestack[-1])

   source(filename, ...)
}

and then examine filenamestack[1] to find the name of the file currently 
being sourced.  (But Source() won't work on connections, only on filenames.)


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] How to find the path or the current file?

2009-03-24 Thread Gabor Grothendieck
See:

https://stat.ethz.ch/pipermail/r-help/2009-January/184745.html

On Tue, Mar 24, 2009 at 7:16 AM, Marie Sivertsen mariesiv...@gmail.com wrote:
 Dear useRs,

 I have a collection of source file and some of these call others.  The files
 are distribute among a number of directories, and to know how to call some
 other file they need to know what file is currently executed.

 As example, I have a file 'search.R' located in directory 'bin' which needs
 to access the file 'terms.conf' located in the directory 'conf', a sibling
 of 'bin'.  I can have somethings like readLines('../conf/terms.conf') in
 search.R, but this work only if search.R is executed from bin, when getwd is
 'bin'.  But when search.R calls from the parent as bin/search.R or any other
 derectory then R complains that it could not find the file
 '../conf/terms.conf'.

 So my questions is:  how can the file search.R, when executied, discover its
 own location and load terms.conf from location of
 search.R/../conf/terms.conf?  the location of search.R can be unrelated to
 the current directory.

 Mvh.
 Marie

        [[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.


__
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] How to find the path or the current file?

2009-03-24 Thread Marie Sivertsen
Thank you Gabor and Duncan for your replys.

Mvh.
Marie


On Tue, Mar 24, 2009 at 1:00 PM, Gabor Grothendieck ggrothendi...@gmail.com
 wrote:

 See:

 https://stat.ethz.ch/pipermail/r-help/2009-January/184745.html

 On Tue, Mar 24, 2009 at 7:16 AM, Marie Sivertsen mariesiv...@gmail.com
 wrote:
  Dear useRs,
 
  I have a collection of source file and some of these call others.  The
 files
  are distribute among a number of directories, and to know how to call
 some
  other file they need to know what file is currently executed.
 
  As example, I have a file 'search.R' located in directory 'bin' which
 needs
  to access the file 'terms.conf' located in the directory 'conf', a
 sibling
  of 'bin'.  I can have somethings like readLines('../conf/terms.conf') in
  search.R, but this work only if search.R is executed from bin, when getwd
 is
  'bin'.  But when search.R calls from the parent as bin/search.R or any
 other
  derectory then R complains that it could not find the file
  '../conf/terms.conf'.
 
  So my questions is:  how can the file search.R, when executied, discover
 its
  own location and load terms.conf from location of
  search.R/../conf/terms.conf?  the location of search.R can be unrelated
 to
  the current directory.
 
  Mvh.
  Marie
 
 [[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.
 


[[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] How to find the path or the current file?

2009-03-24 Thread William Dunlap
I sometimes deal with this problem by putting all the
scripts into a package (in the inst/ directory if they
don't fit elsewhere, perhaps in demo/) and then have
them all use system.file(package=myPkg,...) to locate
the related files.  E.g.,
terms.conf - system.file(package=myPkg,conf,terms.conf)
if (terms.conf==)
 stop(Cannot find  , file.path(conf,terms.conf),  in
package , myPkg)
source(terms.conf)

(It would be nice if system.file itself had an option
to halt if no file could be found but one can write a wrapper
for that.)

Putting all the scripts in a package also makes them easier
to deploy on other machines.  By using the Depends: line
in the DESCRIPTION file you can arrange for packages to
have a core set of scripts available in a standard place.

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com 


[R] How to find the path or the current file?

Marie Sivertsen mariesivert at gmail.com 
Tue Mar 24 12:16:22 CET 2009

Dear useRs,

I have a collection of source file and some of these call others.  The
files
are distribute among a number of directories, and to know how to call
some
other file they need to know what file is currently executed.

As example, I have a file 'search.R' located in directory 'bin' which
needs
to access the file 'terms.conf' located in the directory 'conf', a
sibling
of 'bin'.  I can have somethings like readLines('../conf/terms.conf') in
search.R, but this work only if search.R is executed from bin, when
getwd is
'bin'.  But when search.R calls from the parent as bin/search.R or any
other
derectory then R complains that it could not find the file
'../conf/terms.conf'.

So my questions is:  how can the file search.R, when executied, discover
its
own location and load terms.conf from location of
search.R/../conf/terms.conf?  the location of search.R can be unrelated
to
the current directory.

Mvh.
Marie

__
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.