[R] R init file and source()

Allin Cottrell cottrell at wfu.edu
Tue Aug 5 01:50:04 CEST 2008


In the context of calling R from another program (namely gretl, 
http://gretl.sourceforge.net ) I'm trying to understand the 
interactions of the R init file (corresponding to the environment 
variable RPROFILE) and the source() function.

I'll illustrate my problem with the following simplified contrast
implemented in the bash shell (with R 2.7.1).

1. Works fine:

allin at myrtle:~/Rfoo$ cat fooProfile1
# nothing here
allin at myrtle:~/Rfoo$ cat doit.1
export RPROFILE="fooProfile1"
R --no-save --no-init-file --no-restore-data --slave <fooSrc
allin at myrtle:~/Rfoo$ cat fooSrc 
library(graphics)
postscript("myfile.ps")
allin at myrtle:~/Rfoo$ sh doit.1

(silently produces a well-formed but empty myfile.ps)

2. Doesn't work:

allin at myrtle:~/Rfoo$ cat fooProfile2
source("fooSrc", verbose=TRUE, echo=TRUE)
allin at myrtle:~/Rfoo$ cat doit.2
export RPROFILE="fooProfile2"
R --no-save --no-init-file --no-restore-data --slave
allin at myrtle:~/Rfoo$ cat fooSrc 
library(graphics)
postscript("myfile.ps")
allin at myrtle:~/Rfoo$ sh doit.2
'envir' chosen:<environment: R_GlobalEnv>
encoding = "native.enc" chosen
--> parsed 2 expressions; now eval(.)ing them:

>>>> eval(expression_nr. 1 )
                 =================

> library(graphics)
curr.fun: symbol library
 .. after `expression(library(graphics))'

>>>> eval(expression_nr. 2 )
                 =================

> postscript("myfile.ps")
Error in eval.with.vis(expr, envir, enclos) : 
  could not find function "postscript"

== end of output ==

I'd like to understand why, after loading the graphics library, 
the function postscript() is not available in the second case 
while it is in the first.  (The file fooSrc has the same content 
in the two cases: in the first case it's loaded via the shell, 
while in the second it's loaded using R's source() function.)

Thanks for any help.

-- 
Allin Cottrell
Department of Economics
Wake Forest University, NC



More information about the R-help mailing list