[Rd] inline C/C++ in R: question and suggestion
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 <- " SEXP res;"
> code <- " int nprotect = 0, nx, ny, nz, x, y;"
> code <- " PROTECT(res = Rf_duplicate(a)); nprotect++;"
> code <- " nx = INTEGER(GET_DIM(a));"
> code <- " ny = INTEGER(GET_DIM(a));"
one could use (for example):
code <- '''
int nprotect = 0, nx, ny, nz, x, y;
PROTECT(res = Rf_duplicate(a)); nprotect++;
nx = INTEGER(GET_DIM(a));
ny = INTEGER(GET_DIM(a));
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.
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.
More information about the R-devel