[Rd] Need help on calling Head from C

Radford Neal radford at cs.toronto.edu
Thu Jun 26 22:36:24 CEST 2014


>> PROTECT(dfm=lang3(install("data.frame"),df,ScalarLogical(FALSE)));
>> SET_TAG(CDDR(dfm), install("stringsAsFactors")) ;
>> SEXP res = PROTECT(eval(dfm,R_GlobalEnv));
>> PROTECT(head=lang3(install("head"),res,ScalarInteger(1)));
>> head  = PROTECT(eval(head,R_GlobalEnv));
>> 
>> 
>> I tried the above following , it seemed to be not working . Can you please
>> help.
>> 
>
>Can you elaborate? The above code works AFAICT ...

The code is actually not safe.  Both "install" and "SalarLogical/Integer"
potentially allocate memory, so at least one needs to be protected before
callling lang3.  (Passing one such argument would be OK, since lang3 
protects its arguments, but it doesn't get a chance to do that while they
are still being evaluated.)

Now, I'm not sure this is the source of the actual problem, since both
"data.frame" and "head" presumably already exist, so the install won't
actually allocate memory.  But this is not a safe coding method in 
general.

   Radford Neal



More information about the R-devel mailing list