cubelyr icon indicating copy to clipboard operation
cubelyr copied to clipboard

Bad conversion of list measure to tibble

Open banbh opened this issue 1 year ago • 0 comments

If one of the measures consists of lists then converting the cube to a tibble causes problem. For example:

X <- array(lapply(1:4, seq_len), dim = c(2,2))
X_cube <- tbl_cube(dimensions = list(A = c('foo','bar'), 
                                     B = c('x','y')), 
                   measures = list(x = X))

Tmp <- as_tibble(X_cube)
print(Tmp) # Error in `vec_slice()`

The problem seems to be that the resulting tibble has a column that keeps its dim attribute. That seems to be caused by as.vector preserving the attribute (which is mentioned in help("as.vector")) in https://github.com/hadley/cubelyr/blob/master/R/cube.R#L180

A possible fix is to simply replace L180 with:

mets <- lapply(x$mets, \(m) if (is.list(m)) m[seq_along(m)] else as.vector(m))

(This was originally reported in https://stackoverflow.com/questions/61877176/problem-converting-tbl-cube-with-list-measure-into-a-tibble)

banbh avatar Jun 01 '23 15:06 banbh