[R] Calling S-Plus routines from C or Fortran

Simon Fear fears at roycastle.liv.ac.uk
Fri Jul 9 15:49:24 CEST 1999


Ray,

what you've been told is true but pessimistic. On the bright side you
*can* use call_R in your C code, provided you call your C program from a
running R session. Your R session need only be this long:

> dyn.load('myprogram')
> .C('myprogram',arg1,arg2,...)

There is an alternative. Because *all* the R source code is available,
you could strip in the relevant routine and compile it from source. This
won't work for R routines that depend on big things like the interpreter
running, but simple functions could be called this way. Warning: apart
from its limitations, I'm sure this would violate the licence agreement.
It's not an option if you're intending to distribute your code.

By the way, I have never found a good reason for using call_R(). Mostly
you go into C to avoid looping within the R interpreter, and the
temptation is to write a C loop with the convenience of an R function
body. But forget it; it's not actually quicker, ever. The other
situation in which call_R looks tempting is that in the middle of some C
code you want to do something rather complex, such as fitting a linear
model. However, rather than use call_R, which really just calls an
embedded R session, it's best to just split your C code in two, so your
R function has

ans1 <- .C('firsthalf',...)
lm(y~x,data=as.data.frame(ans1))
ans2 <- .C('secondhalf',...)

avoiding the embedding.

Simon

> -----Original Message-----
> From: Ray Gallagher [mailto:R.Gallagher at csc.liv.ac.uk]
> Sent: 09 July 1999 13:47
> To: 'Simon Fear'
> Subject: RE: [R] Calling S-Plus routines from C or Fortran
> 
> 
> Ah this seems to conflict with what has come back so far Simon.
> 
> The information so far has been that you can only call C or 
> Fortran from S-Plus or R
> 
> See text below in quotes from Douglas Bates ( not sure if 
> this is what you meant to say Simon?
> 
> "In general, no.
> There is a mechanism using a C function call_R (also available as
> call_S, for historical reasons) to evaluate an expression in R from
> within a C function.  This mechanism only works, however, when the C
> function has itself been called from R.  It needs the runtime R
> facilities to be working at the time it is called.  Thus the sequence
> is 
> 1)	R evaluates an R expression
> 2)	This evaluation calls a C function through .C and 
> passes another R expression.
> 3)	The C function (usually an iterative function for 
> optimization or something like that) sets up some values of 
> parameters then calls call_R 
> 
> If what you are looking for is a general embedding mechanism, 
> it does not currently exist."
> Regards
> 
> Ray
> 
> 
> -----Original Message-----
> From:	Simon Fear [SMTP:fears at roycastle.liv.ac.uk]
> Sent:	09 July 1999 10:45
> To:	'Ray Gallagher'
> Subject:	RE: [R] Calling S-Plus routines from C or Fortran
> 
> Ray,
> 
> I could have told you the answer to this when I saw you on 
> Wednesday if
> you'd had a chance to ask ...
> 
> Anyway, yes, read the Splus documentation, then do the same in R.
> Basically, look at the R source for the name of the routine, 
> and call it
> in the appropriate wrapper function. RTFM, as they say ... (hint: look
> for call_R)
> 
> Simon
> 
> cc to the list per Spiros' request.
> 
> > -----Original Message-----
> > From: Ray Gallagher [mailto:R.Gallagher at csc.liv.ac.uk]
> > Sent: 07 July 1999 16:13
> > To: 'r-help at stat.math.ethz.ch'
> > Subject: [R] Calling S-Plus routines from C or Fortran
> > 
> > 
> > I am told it is common practice to call C/Fortran routines 
> > from one's S-PLUS/R. However is it possible to call S-Plus 
> > routines into C and Fortran programs using them as executable 
> > functions/subroutines ?
> > 
> > Grateful for any answers to this.
> > 
> > Regards
> > 
> > Ray
> > 
> > P.S. Please direct emails directly to rayg at csc.liv.ac.uk as I 
> > am not on the mailing list.
> > 
> > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> > -.-.-.-.-.-.-.-.-
> > 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
> > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> > _._._._._._._._._
> > 
> 
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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