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

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Tue, 22 May 2007 15:52:42 -0400

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



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 22 May 2007 - 19:56:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 May 2007 - 17:33:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.