[R] unix environment variables under R

Roger Bivand Roger.Bivand at nhh.no
Tue Oct 29 15:22:26 CET 2002


> Jason Turner wrote:
>> On Tue, Oct 29, 2002 at 10:11:09AM +0100, Jan Malte Wiener wrote: ...
>>
>>>thought a UNIX environment variable could be the solution, but R
>>> doesn't  seem to know about the environment variables.
>>>e.g. >> source("$PROJECT/xxx.R") results in
>>> >> Error in file(file, "r") : unable to open connection
>>>
>>>is there something like environment variables for R?
>>
>>
>> Yep.  Sys.getenv()
>>
>> Cheers
>>
>> Jason
>
> thanks for your answer,
> but how do i source a file using that environment variable; if the
> environment variable holds a path to a directory and i want to source a
> file within that directory??
> e.g.:
> Sys.putenv("ABC"="~/R-Stuff/")
> in ~/R-Stuff there a bunch of R-files
> now i want to source one of these R-files using the ABC environment
> variable.
> sorry, but i do not know how to do this ?

Using your initial example, the shell variable PROJECT is set before you
start R, isn't it?

$ PROJECT=~/tmp ; export PROJECT
$ echo $PROJECT
/home/zzz/tmp
$ R
> proj <- Sys.getenv("PROJECT")
> proj
        PROJECT
"/home/zzz/tmp"
> source(paste(proj, "/xxx.R", sep=""))

or at your choice:

> getwd()
[1] "/home/xxx"
> setwd(proj)
NULL
> getwd()
[1] "/home/xxx/tmp"
> source("xxx.R")

This are two answers to your first question, but your response makes it
look as though this wasn't your actual question, since you seem to know
that the files are in "~/R-Stuff" anyway? I think you have many machines,
with the R files in different directories for different users. Then
defining the shell variable PROJECT for each user on the various machines
to point to the correct directory for that user and machine should fix it,
as Jason pointed out. Then use Sys.getenv() to retrieve the value of the
shell variable, as shown above.

Roger

-- 
Roger Bivand
NHH, Breiviksveien 40, N-5045 Bergen, Norway
(travelling but still accessible)


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list