Matthew Fidler
2024-Jun-25 09:25 UTC
[Rd] SET_TYPEOF no longer allowed, how should I call R from within C?
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]]
Tomas Kalibera
2024-Jun-25 12:35 UTC
[Rd] SET_TYPEOF no longer allowed, how should I call R from within C?
On 6/25/24 11:25, Matthew Fidler wrote:> 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?Please check the current Writing R Extensions from the official location. It has an updated version of the example using allocLang. https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Evaluating-R-expressions-from-C https://cran.r-project.org/doc/manuals/r-devel/R-exts.html#Evaluating-R-expressions-from-C Best Tomas> > > Thanks in advance. > > Matt > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Duncan Murdoch
2024-Jun-25 13:57 UTC
[Rd] SET_TYPEOF no longer allowed, how should I call R from within C?
On 2024-06-25 5:25 a.m., Matthew Fidler wrote:> 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.htmlThat was written in 2017. You should use the one that came with R, or even better, the one that comes with the development version of R. Duncan Murdoch> > 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]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel