[R] Sending "..." to a C external

Douglas Bates bates at stat.wisc.edu
Sat Aug 23 17:46:04 CEST 2008


On Fri, Aug 22, 2008 at 2:16 PM, John Tillinghast <tilling at gmail.com> wrote:
> I'm trying to figure this out with "Writing R Extensions" but there's not a
> lot of detail on this issue.
> I want to write a (very simple really) C external that will be able to take
> "..." as an argument.
> (It's for optimizing a function that may have several parameters besides the
> ones being optimized.)

> I got the "showArgs" code (from R-exts) to compile and install, but it only
> gives me error messages when I run it. I think I'm supposed to pass it
> different arguments from what I'm doing, but I have no idea which ones.

> What exactly are CAR, CDR, and CADR anyway? Why did the R development team
> choose this very un-C-like set of commands? They are not explained much in
> R-exts.

Emmanuel has answered that - very engagingly too.  On the train from
Dortmund to Dusseldorf last week I was describing exactly that
etymology of the names CAR, CDR, CDDR, ... to my companions but I got
it wrong.  I had remembered CDR as "contents of the data register" but
Emmanuel is correct that it was "contents of the decrement register".

I don't think it is necessary to go through the agonies of dealing
with the argument list in a .External call., which is what I assume
you are doing. (You haven't given us very much information on how you
are trying to pass the ... argument and such information would be very
helpful.  The readers of this list are quite intelligent but none, as
far as I know, have claimed to be telepathic.)

The way that I would go about this is using .Call in something like

.Call("myCfunction", arg1, arg2, dots = list(...), PACKAGE = "myPackage")

Then in your C code you check the length and the names of the dots
argument and take appropriate action.

An alternative, if you want to use the ... arguments in an R
expression to be evaluated by your optimizer, is to create an
environment, assign the elements of list(...) to the appropriate names
in that environment and pass the environment through .Call to be used
as the evaluation environment for your R expression.  There is a
somewhat complicated example of this in the nlmer function in the lme4
package which you can find at http://lme4.r-forge.r-project.org/.
However, I don't feel embarrassed about the example being complicated.
 This is complex stuff and it is not surprising that it isn't
completely straightforward to accomplish. If you feel that this is
opaque in R i can hardly wait to see what you think about writing the
SPSS version.



More information about the R-help mailing list