R-beta: loadable modules

Thomas Lumley thomas at biostat.washington.edu
Mon Jun 1 19:36:20 CEST 1998

On Fri, 29 May 1998, Jean Meloche wrote:

> I want to write a C function that should return
> an object of unknown structure at the time of the
> invocation.
> In particular, I'd like to
> 1- return a string of unknown length
> 2- a vector or a matrix of unknown dims and of
>    unknown element type
> 3- a data frame
> Is any of that possible? Are there examples?
> Which R functions should I call to create such
> objects?
It's possible, but tricky.  You need to call R code that creates the
objects _after_ you know what they look like but _before_ you pass them
back.  One way to do this for an array is as follows
- Call C code to create the array, storing it in a variable declared as
static, so that it won't go away when you return to R. Use malloc, rather
than R_alloc so that it won't get tidied up by R.
- Return the dimensions and type of the array to R
- From R, call another C function, with one argument being a suitable
- This C function just returns the array.

You could call the internal R functions that create objects, bypassing the
middle two steps. This would be a lot harder to program, as there isn't
much documentation (read the source), and might break in future versions
of R. It would be more memory-efficient, though.

Thomas Lumley
Biostatistics		: "Never attribute to malloc(3) what  :
Uni of Washington	:  can be adequately explained by     :
Box 357232		:  incompetence"                      :
Seattle WA 98195-7232	:				      :

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