[Rd] bypassing SET_DIM checking

Hin-Tak Leung hin-tak.leung at cimr.cam.ac.uk
Mon Aug 14 13:08:42 CEST 2006


Hi,

I am not going to work out why you code crashes, but I did manage to 
bypass dim checking some time ago - and as others had replied, it is
Not a Good Idea (TM) , as the check is there for a reason. If you really 
want to do it, here is the relevant snipplet of what I did (oh, I did 
copy installAttrib() from R/src/main/attrib.c - it is declared static
there so you can cut-and-paste the whole routine to your code) - but
as others had said, it is Not a Good Idea:

  ...
SEXP ans, dim, dimnames, class;
...
PROTECT(dim = allocVector(INTSXP, 2));
...
   INTEGER(dim)[0] = i;
   INTEGER(dim)[1] = j;
...
PROTECT(dimnames = allocVector(VECSXP, 2));
SET_VECTOR_ELT(dimnames, 0, R_NilValue);
SET_VECTOR_ELT(dimnames, 1, R_NilValue);
...
/* we bypassed the checks for Dimname and Dim */
installAttrib(ans, R_DimNamesSymbol, dimnames);
installAttrib(ans, R_DimSymbol, dim);
...
return ans;
...

Hin-Tak Leung

miguel manese wrote:
> Hello,
> 
> I created an S3 "type" w/c is actually a list that should look like a
> matrix but whose data is actually in an external file. The SEXP just
> contains info on how to retrieve the data from the file. When I get to
> the part of creating the SEXP, I found out that SET_DIM & SET_DIMNAME
> checks the SEXP if the length matches (somewhere in src/main/attrib.c,
> dimgets & dimnamegets IIRC (it's on my other partition. life sucks)).
> After a quick look at the R code, I tried to bypass them by doing:
> 
> static void _installAttrib(SEXP obj, SEXP name, SEXP value) {
>     /* to bypass dim & dimnames property checking */
>     SEXP attr, cur_attr;
>     PROTECT(attr = NEW_LIST(1));
>     SETCAR(attr, value);
>     SET_TAG(attr, name);
>     if (ATTRIB(obj) == R_NilValue) SET_ATTRIB(obj, attr);
>     else {
>         cur_attr = nthcdr(ATTRIB(obj), length(ATTRIB(obj)) - 1);
>         SETCDR(cur_attr, attr);
>     }
>     UNPROTECT(1);
> }
> 
> but it crashes R. Is there any other way to do this other than copying
> those code in R that was not included in the API ?
> 
> Thanks,
> M. Manese
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list