[R] R Interface handholding

Pat Gunn pgunn at cs.cmu.edu
Tue Dec 9 19:50:54 CET 2003


Hello,
I need a bit of handholding with R, specifically, with writing
packages for it. I'm a systems programmer, and am, on the request
of several users of our software, working on generating R interfaces.
For starters, I've written the following R function (which compiles):

SEXP myincr(SEXP Rinput)
{ // Returns input integer incremented by one
int input;
SEXP returner;

PROTECT(Rinput = AS_NUMERIC(Rinput));

input = * INTEGER(Rinput);
input++;
PROTECT(returner = NEW_INTEGER(input));
Rprintf("Hey there\n");
return returner;
}

I've made this into a package, by dropping it into a stub directory
along with something called init.c:

#include "areone.h"
#include <R_ext/Rdynload.h>
#include <Rinternals.h>

R_NativePrimitiveArgType myincr_t[1] = {INTSXP};

static const R_CMethodDef cMethods[] =
        {
        {"myincr", (DL_FUNC) &myincr, 1, myincr_t}
        };

void R_init_myincr(DllInfo* dll)
{
R_registerRoutines(dll, cMethods, NULL, NULL, NULL);
}

R is happy to install this for me, but after doing a library(myincr),
the function doesn't seem to be available, so I presume I'm missing
something. Does R normally call, at library load, R_init_$MODULENAME() ?


My other question is.. our software produces data structures (we call datsets)
which resemble limited database tables, and I'd like some advice on exposing
them to R -- columns, in our scheme, either hold doubles or strings,
the columns have names, and we need the ability to export these into
appropriate R structures as well as populate them from R. I notice that
the R DBI, at least as according to its documentation, uses the database
to hold the data (presumably in temporary tables) and returns parts of it
as requested, via R functions. I could use a static global pointer (in C) into a
storage space of datsets, and write bridge functions exporting them as R arrays,
or I could attempt to find an appropriate native format and export to it..
any advice?

It might be worth mentioning that all of the code to do this will
eventually be auto-generated -- we already have code to do this for C
that doesn't need to expose people linking our code to all of our
custom structures and stuff.

-- 
Pat Gunn
Research/Systems Programmer, Auton Group, CMU




More information about the R-help mailing list