[Rd] Two ALTREP questions

Jiefei Wang @zwj|08 @end|ng |rom gm@||@com
Sat Nov 21 13:56:59 CET 2020


Hello,

I have two related ALTREP questions. It seems like there is no way to
assign attributes to an ALTREP vector without using C++ code. To be more
specifically, I want to make an ALTREP matrix, I have tried the following R
code but none of them work.
```
.Internal(inspect(1:6))
.Internal(inspect(matrix(1:6, 2,3)))
.Internal(inspect(as.matrix(1:6)))
.Internal(inspect(structure(1:6, dim = c(2L,3L))))
.Internal(inspect({x <- 1:6;attr(x, "dim") <- c(2L,3L);x}))
.Internal(inspect({x <- 1:6;attributes(x)<- list(dim = c(2L,3L));x}))
```

The only way to make an ALTREP matrix is to use the C level function
```
attachAttrib <- inline::cxxfunction( signature(x = "SEXP", attr = "SEXP" )
, '
SET_ATTRIB(x,attr);
return(R_NilValue);
')
x <- 1:6
attachAttrib(x, pairlist(dim = c(2L, 3L)))
.Internal(inspect(x))
```

Since the matrix, or adding attributes, is a common need for the object
operation, I wonder if this missing feature is intended? This also brings
my second question, it seems like the ALTREP coercion function does not
handle attributes correctly.  After the coercion, the ALTREP object will
lose its attributes.
```
coerceFunc <- inline::cxxfunction( signature(x = "SEXP", attr = "SEXP" ) , '
SET_ATTRIB(x,attr);
return(Rf_coerceVector(x, REALSXP));
')
> coerceFunc(1:6, pairlist(dim = c(2L, 3L)))
[1] 1 2 3 4 5 6
> coerceFunc(1:6 + 0L, pairlist(dim = c(2L, 3L)))
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
```
The problem is that the coercion function is directly dispatched to the
user-defined ALTREP coercion function, so the user is responsible to attach
the attributes after the coercion. If he forgets to do so, then the result
is a plain vector. Similar to the `Duplicate` and `DuplicateEX` functions
where the former one will attach the attributes by default, I feel that the
`Coerce` function should only return a plain vector and there should be a
`CoerceEx` function to do the attribute assignment, so the logic in the
no-EX ALTREP functions can be consistent. I do not know how dramastic the
change would be, so maybe this is too hard to do.

BTW, is there any way to contribute to the R source? I know R has a limited
resouces, so if possible, I will be happy to fix the matrix issue myself
and make some minor contributions to the R community.

Best,
Jiefei

	[[alternative HTML version deleted]]



More information about the R-devel mailing list