Skip to content

relative referencing for filenames &etc

6 messages · Greg Tarpinian, Uwe Ligges, Brian Ripley +1 more

#
BACKGROUND:
I use SAS on a daily basis and one of its most powerful features in a
production environment is the use of LIBNAME and FILEREF statements, e.g.:


PROC PRINTTO LOG = "&LOGDIR.\data processing.log" NEW;
RUN;
PROC IMPORT 
	DATAFILE= "&DATADIR.\blah.xls"
	OUT = TEMP
	DBMS = EXCEL REPLACE;
    SHEET = "Sheet1"; 
    GETNAMES = YES;
RUN;


Properly setting up SAS Shortcuts in the WinXP environment, or (say)
the UNIX equivalent of batch files in the UNIX environment will cause
SAS to autoexecute (say) an INIT.SAS file that automatically assigns
the LOGDIR and DATADIR macro variables used above.  This is extremely
convenient because it makes the SAS code more portable from project to
project.


MY QUESTION:
Is it possible to use this kind of relative file and directory referencing
from within R?


Kind regards,


      Greg
#
On Mon, 27 Feb 2006, Greg Tarpinian wrote:

            
Perhaps you are looking for ?setwd

Uwe ligges
#
On Tue, 28 Feb 2006, Uwe Ligges wrote:

            
Another idea is to use environment variables.  So if in an R function I 
have

     xls_file <- file.path(Sys.getenv("DATADIR"), "blah.xls")

I can R run from a shortcut, appending DATADIR=J:/foo/bar to the command 
line.
#
Thank you both for your helpful suggestions.  My ignorance of R prevents me
from understanding exactly how to

   "...run from a shortcut, appending DATADIR=J:/foo/bar to the 
    command line"

but I assume that Appendix C of "S Programming" will help me there -- I was
unaware of this material when I posted my question.  I was able to success-
fully use the following:

   > Sys.putenv("R_DATADIR"="Z:/.../raw")
   > Sys.getenv("R_DATADIR")
      R_DATADIR 
   "Z:/.../raw" 
   > file.path(Sys.getenv("R_DATADIR"), "RAWDATA.xls")
   [1] "Z:/.../raw/RAWDATA.xls"

Using this type of code, I should be able to "point" R at data and output
directories.  In my industry, programmers are required to save their SAS
log files for auditing purposes, and knowing that R has similar capabilities
is very helpful.

Again, thanks for your help.  Kind regards,


     Greg
--- Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:

            
#
On Tue, 28 Feb 2006, Greg Tarpinian wrote:

            
See the rw-FAQ for more details.

  
    
#
I think the idea of the prior respondents was that R_DATADIR
would be set outside R and the application and just fetched from
an environment variable inside the application so that the application
is independent of it.

If, in fact, your R code knows the data directory anyways, you could
just do

  datadir <- "J:/foo/bar"
  file.path(datadir, "rawdata.xls")
On 2/28/06, Greg Tarpinian <sasprog474474 at yahoo.com> wrote: