ripley at stats.ox.ac.uk
2008-Mar-31 08:10 UTC
[Rd] (PR#11054) "Writing R Extensions": bad example with CAR /
But it is not taken 'verbatim from src/main/print.c' (at least not in that version of R), and the code is not run with USE_RINTERNALS defined when write-barrier checking is enabled. The example has been updated to match the current code in 2.7.0 alpha. On Sun, 30 Mar 2008, sanders at fs.tum.de wrote:> Full_Name: Simon Anders > Version: 2.6.2 > OS: Ubuntu Linux > Submission from: (NULL) (86.22.75.91) > > > This is a rather minor "documentation bug", certainly not at all urgent. > > The manual "Writing R extensions" explains in section 5.10 ("Evaluating R > expressions from C") how to use the eval function. Without going into much > details, an example is given how to build up a LANGSXP pairlist to represent an > expression. > > This example is taken at verbatim from src/main/print.c, and uses CAR and CDR as > l-values, e.g. > CAR(t) = install("print") > > This has just confused me quite a bit, as my own code, which I wrote following > this example, did not compile, complaining about 'CAR(t)' being illegal as > l-value. > > After a while I figured out the reason, which is (as you probably have spotted > immediatly) that the example is taken from a code snipped that imports > Rinternals.h with defined USE_RINTERNALS, while I followed the manual's advice > to not define USE_RINTERNALS for extensions. > > Maybe you could add a note in this section of the manual that one should use > SETCAR and SETCDR instead. A more illustrative example would be even better. > > > Just in case you want to use it as example, here is my code. I hope it is > correct in your expert eyes. > > /* Remove the variable with name s from the environment ev, > i.e. construct the call: rm( list=s, envir=ev ) */ > > SEXP call; > PROTECT( call = Rf_allocList( 3 ) ); > SET_TYPEOF( call, LANGSXP ); > > SETCAR( call, install( "rm" ) ); > > SETCAR( CDR(call), allocVector( STRSXP, 1 ) ); > SET_STRING_ELT( CAR( CDR(call) ), 0, mkChar( s ) ); > SET_TAG( CDR(call), install("list") ); > > SETCAR( CDDR(call), ev ); > SET_TAG( CDDR(call), install("envir") ); > > eval( call, R_GlobalEnv ); > UNPROTECT( 1 ); > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
ripley at stats.ox.ac.uk
2008-Mar-31 18:40 UTC
[Rd] (PR#11054) "Writing R Extensions": bad example with CAR /
On Mon, 31 Mar 2008, Simon Anders wrote:> Dear Prof Ripley, > > Prof Brian Ripley wrote:>> But it is not taken 'verbatim from src/main/print.c' (at least not in that >> version of R), and the code is not run with USE_RINTERNALS defined when >> write-barrier checking is enabled. >> >> The example has been updated to match the current code in 2.7.0 alpha. > > I just assumed that it is copied verbatim without checking because it says so > in the sentence above the code ("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.")That was true once, long ago.> I don't understand, however, what you mean by stating that USE_RINTERNALS is > not needed (and I have to admit that I have no knowledge of this > 'write-barrier checking' mechanism.)I didn't say 'USE_RINTERNALS is not needed' for your code: I said it was not used for the code that was run. ... -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Reasonably Related Threads
- "Writing R Extensions": bad example with CAR / CDR as lvalues (PR#11054)
- (PR#11054) "Writing R Extensions": bad example with CAR
- Correction to vec-subset speed patch
- Control statements with condition with greater than one should give error (not just warning) [PATCH]
- Calling R functions with multiple arguments from C