[R] how to run R as a daemon

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon May 19 14:48:15 CEST 2003

On Mon, 19 May 2003, Laurent Faisnel wrote:

> I  just visited the link you gave me. One of the problems that could 
> occur using NuSOAP is that I do not intend using Python. Thanks anyway. 
> I would also like to ask more precise questions :

`more precise'?  At least some of the inefficiencies are emerging, but I
still have little picture of what you are actually trying to do.  It ought
to be possible to run a new R session in a well under a second (on a 1+GHz

gannet% time env R_DEFAULT_PACKAGES=NULL R --vanilla --slave < /dev/null
0.203u 0.039s 0:00.27 85.1%     0+0k 0+0io 1220pf+0w

for example (on a dual Athlon 2600, R 1.7.0).

> - is it possible to run R as a standalone service, which runs in 
> background indefinitely, waiting for instructions ? 

It is, but waiting for instructions from one place (stdin or on a socket).
There is some danger of different jobs sent leaving things behind that 
will cause interactions.

> This would be a 
> great improvement for me, since each time it is launched R has to 
> re-load a (constant) matrix generated thanks to a database connection 
> (takes a very long while)

You could just save and then load that matrix in your .RData

> - my R script is object-oriented; I define classes and methods, but I'm 
> not so sure about how methods should be declared; I usually write 
> something like :
> mymethod <- function(.Object) UseMethod("mymethod",.Object);
> setMethod("mymethod","myclass",
>         function(.Object)
>         {
>             # instructions
>             return(.Object);
>         }
>         );
> Perhaps is this not the best way to write methods ? Could this explain 
> the following fact : when I run the script, R spends about 1/3 of its 
> thinking time creating the generic functions linked with my methods. 
> Isn't this wasted time ? The same script may be executed many times 
> consecutively by different users, and each time R has to re-define the 
> generic functions ! (are always the same)

Yes, but it is your usage that is the problem.  You are mixing S3 and S4
methods (where did you copy an example like that from?).  You should
dump your scripts if you use S4 methods (which I don't think you should be
doing in such simple examples, nor if you care about speed):  see the
examples of S4-using packages on CRAN (e.g. DBI, SparseM).

Also consider upgrading to R 1.7.0 and setting R_DEFAULT_PACKAGES to what 
you actually need (and omit methods if you don't need it)

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