[R] Returning a dataframe from a C function (How) ?

Thomas Lumley tlumley at u.washington.edu
Thu May 2 18:32:32 CEST 2002

On Thu, 2 May 2002, Neil Osborne wrote:

> I'm writing an R extension. The language I'm using is ANSI C. One of the
> functions I'm writing will accept a string and return a dataset as a
> dataframe. i.e the prototype will be of the form:
> dataframe foo(R_String)
> {
> }
> Does anyone have any skeleton code for such a function, i.e. one that
> receives an R "string" and returns a dataset as a dataframe ?
> The main concerns I have are:
> 1. Handling the "R_String" in C (i.e. converting it into a char*)
> 2. Creating an R dataframe in C

You could look at the code in foreign, which takes file names and returns
data frames.

For example,


 fp = fopen(R_ExpandFileName(CHAR(STRING_ELT(fname,0))), "rb");

showing the use of CHAR(STRING_ELT(fname,0)) to extract a char * from an R

and foreign/src/stataread.c:R_LoadStataData creates a data frame and
passes it back to R

foreign/src/SASxport.c: xport_read is shorter and may be easier to read.

It actually returns a list, which is coerced to a dataframe in interpreted


Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle

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