[Rd] SET_TYPEOF no longer allowed, how should I call R from within C?

Matthew Fidler m@tthew@||d|er @end|ng |rom gm@||@com
Tue Jun 25 11:25:19 CEST 2024


Hi,

I have adapted code to run R from within C from the writing R extensions
here

https://colinfay.me/writing-r-extensions/system-and-foreign-language-interfaces.html

As a more comprehensive example of constructing an R call in C code and
evaluating, consider the following fragment of printAttributes in
src/main/print.c.

    /* Need to construct a call to

       print(CAR(a), digits=digits)

       based on the R_print structure, then eval(call, env).

       See do_docall for the template for this sort of thing.

    */

    SEXP s, t;

    t = s = PROTECT(allocList(3));

    SET_TYPEOF(s, LANGSXP);

    SETCAR(t, install("print")); t = CDR(t);

    SETCAR(t, CAR(a)); t = CDR(t);

    SETCAR(t, ScalarInteger(digits));

    SET_TAG(t, install("digits"));

    eval(s, env);

    UNPROTECT(1);

At this point CAR(a) is the R object to be printed, the current attribute.
There are three steps: the call is constructed as a pairlist of length 3,
the list is filled in, and the expression represented by the pairlist is
evaluated.

A pairlist is quite distinct from a generic vector list, the only
user-visible form of list in R. A pairlist is a linked list (with CDR(t)
computing the next entry), with items (accessed by CAR(t)) and names or
tags (set by SET_TAG). In this call there are to be three items, a symbol
(pointing to the function to be called) and two argument values, the first
unnamed and the second named. Setting the type to LANGSXP makes this a call
which can be evaluated.



New checks  tells me that this is no longer allowed since it was not part
of the public api any longer.


So, how does one call R from C then?

Also should the writing R extensions be updated with the new approved
approach?


Thanks in advance.

Matt

	[[alternative HTML version deleted]]



More information about the R-devel mailing list