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

William Dunlap wdunlap at tibco.com
Tue Mar 24 16:32:41 CET 2009


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




More information about the R-help mailing list