99# ' @param points list of \code{\link{PointFrame}}s
1010# ' @param shapes list of \code{\link{ShapeFrame}}s
1111# ' @param tables list of \code{SingleCellExperiment}s
12+ # ' @param data
13+ # ' For \code{Image/LabelArray}s, a list of \code{ZarrArray}s;
14+ # ' For \code{Shape/PointFrame}s, an \code{arrow} Table or derivative.
15+ # ' @param zattrs \code{Zattrs} objects; used to represent .zattrs.
16+ # ' @param metadata (option) list of free-form extra data.
1217# '
1318# ' @return \code{SpatialData}
1419# '
1924# '
2025# ' names(sd)
2126# ' sd@images[[1]]
22- # ' sd["shapes", 2]
27+ # ' sd[-4, ][, 1]
28+ # ' sd[1:2, c(1,1)]
29+ # ' sd["shapes", c(1,3)]
2330# ' region(sd, "table")
2431# '
2532# ' @importFrom S7 new_class new_generic method class_list check_is_S7
@@ -50,10 +57,13 @@ SpatialData <- new_class("SpatialData",
5057 ok <- c(ok , mapply(x = slot , y = type , \(x , y ) {
5158 if (length(z <- attr(self , x )) && ! .all(z , y ))
5259 sprintf(" '@%s' should be a list of '%s's" , x , y )
53- nms <- names(slot(self , x ))
54- len <- vapply(nms , nchar , integer(1 ))
55- if (is.null(nms ) || any(len == 0 ))
56- sprintf(" '@%s' should be a fully named list" , x )
60+ l <- slot(self , x )
61+ if (length(l )) {
62+ nms <- names(l )
63+ len <- vapply(nms , nchar , integer(1 ))
64+ if (is.null(nms ) || any(len == 0 ))
65+ sprintf(" '@%s' should be a fully named list" , x )
66+ }
5767 }, SIMPLIFY = FALSE ) | > unlist())
5868 ts <- self @ tables
5969 for (t in ts ) {
@@ -72,11 +82,47 @@ names(.LAYERS) <- .LAYERS <- names(SpatialData@properties)
7282
7383method(`[[` , SpatialData ) <- \(x , i ) attr(x , .LAYERS [i ])
7484
85+ # ' @importFrom S7 prop_names
86+ # ' @importFrom methods slot
7587method(`[` , SpatialData ) <- \(x , i , j ) {
76- n <- length(y <- x [[i ]])
77- if (missing(j )) if (n ) j <- TRUE else return (y )
78- if (is.numeric(j ) && any(j > n )) stop(" 'j' out of bounds" )
79- if (! isTRUE(j ) && length(j ) == 1 ) y [[j ]] else y [j ]
88+ ps <- prop_names(x )
89+ if (missing(i )) {
90+ n <- vapply(ps , \(. ) length(slot(x , . )), integer(1 ))
91+ i <- ps [n > 0 ]
92+ } else {
93+ if (is.numeric(i )) {
94+ stopifnot(abs(i ) < = length(ps ), i == round(i ))
95+ i <- ps [i ]
96+ } else if (is.character(i )) {
97+ i <- match.arg(i , ps , TRUE )
98+ }
99+ }
100+ if (missing(j )) {
101+ j <- ! logical (length(i ))
102+ } else {
103+ if (length(i ) == 1 ) {
104+ j <- list (j )
105+ } else {
106+ if (length(j ) == 1 )
107+ j <- replicate(length(i ), j , FALSE )
108+ j <- as.list(j )
109+ }
110+ stopifnot(length(j ) == length(i ))
111+ for (. in seq_along(i )) {
112+ .j <- j [[. ]]
113+ l <- slot(x , i [. ])
114+ if (is.character(.j )) {
115+ match.arg(.j , names(l ), TRUE )
116+ } else {
117+ n <- length(l )
118+ stopifnot(abs(.j ) < = n , .j == round(.j ))
119+ }
120+ }
121+ }
122+ l <- mapply(i = i , j = j , \(i , j ) {
123+ slot(x , i )[j ]
124+ }, SIMPLIFY = FALSE )
125+ do.call(SpatialData , l )
80126}
81127
82128method(names , SpatialData ) <- \(x ) lapply(.LAYERS , \(. ) names(slot(x , . )))
0 commit comments