[R] R init file and source()

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Aug 5 05:31:10 CEST 2008


This relates to the order of things being done.  See ?Startup.

Note that the default packages are not loaded until after the Rprofile 
file has been run, and postscript() is in package grDevices, not in 
graphics. Using grDevices::postscript will always work.

It looks to me that you are trying to emulate Rscript (or R -f).

On Mon, 4 Aug 2008, Allin Cottrell wrote:

> 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
>
> ______________________________________________
> R-help at 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list