[Rd] inline C/C++ in R: question and suggestion

Duncan Murdoch murdoch at stats.uwo.ca
Tue May 22 21:52:42 CEST 2007

On 5/22/2007 1:59 PM, Oleg Sklyar wrote:
> This is a question and maybe an announcement.
> We've been discussing in the group that it would be nice to have a
> mechanism for something like "inline" C/C++ function calls in R. I do
> not want to reinvent the wheel, therefore, if something like that
> already exists, please give me a hint -- I could not find anything. If
> not, here is a working solution, please criticise so I could improve it.

This would be nice.

One suggestion that probably doesn't affect your package:  It would be 
even nicer if R incorporated something that Duncan Temple Lang suggested 
last year, namely a new kind of quoting that didn't need escapes in the 
string.  He suggested borrowing triple quotes from Python; I suggested 
something more like heredocs as in shells or Perl, or like \verb in TeX, 
in case you wanted triple quotes in your C function.  It would be nice 
to settle on something, so that instead of

 > code[1] <- "  SEXP res;"
 > code[2] <- "  int nprotect = 0, nx, ny, nz, x, y;"
 > code[3] <- "  PROTECT(res = Rf_duplicate(a)); nprotect++;"
 > code[4] <- "  nx = INTEGER(GET_DIM(a))[0];"
 > code[5] <- "  ny = INTEGER(GET_DIM(a))[1];"

one could use (for example):

code <- '''
    SEXP res;
    int nprotect = 0, nx, ny, nz, x, y;
    PROTECT(res = Rf_duplicate(a)); nprotect++;
    nx = INTEGER(GET_DIM(a))[0];
    ny = INTEGER(GET_DIM(a))[1];

I think the only way this would affect your package is that you need to 
be able to handle embedded newlines in strings, but I can't see why you 
wouldn't be able to do that automatically.

Another suggestion:

You might want to allow a list of signatures and code chunks to be 
passed to the compiler, producing a list of functions to evaluate them, 
all compiled into one DLL.

Duncan Murdoch

More information about the R-devel mailing list