Skip to content

Commit a8d9a13

Browse files
committed
accept R arrays in shape() (synonym for dim())
1 parent 9c968f9 commit a8d9a13

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

R/shape.R

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,29 +130,20 @@ shape <- function(...) {
130130
return(lapply(shp, function(d) d %||% NA_integer_))
131131
}
132132

133-
## TODO: shape(<R array>)
134-
## Users may pass R arrays to shape(), expecting it to behave like dim().
135-
## If we accept them, the edgecase of 1-d arrays gets tricky (esp because
136-
## numpy vectors arrays get converted to 1d R arrays)
137-
## If we accept simple R arrays and treat them the same as Tensors,
138-
## i.e., shape() is synonym for dim(), return dim(x)
139-
# if(!is.object(x) && is.atomic(x) &&
140-
# !is.null(attr(x, "dim", TRUE)))
141-
# return(dim(x))
142-
## or we warn
143-
# if (!is.null(dim(x)) && length(x) > 200)
144-
# warning("Did you pass an R array to shape()? Did you mean to use dim()?")
133+
if(is.array(x))
134+
return(dim(x))
145135

146136
if (is.null(x) ||
147137
identical(x, NA_integer_) ||
148138
identical(x, NA_real_) ||
149139
identical(x, NA) ||
150140
(is.numeric(x) && isTRUE(suppressWarnings(x == -1L))))
151-
NA_integer_ # so we can safely unlist()
152-
else if (!is.atomic(x) || length(x) > 1)
153-
lapply(x, fix)
154-
else
155-
as.integer(x)
141+
return(NA_integer_) # so we can safely unlist()
142+
143+
if (!is.atomic(x) || length(x) > 1)
144+
return(lapply(x, fix)) # recurse
145+
146+
as.integer(x)
156147
}
157148

158149
shp <- unlist(lapply(list(...), fix), use.names = FALSE)

0 commit comments

Comments
 (0)