66# ' @rdname SpatialData
77setMethod ("$ ", "SpatialData", \(x, name) attr(x, name))
88
9+ # ' @exportMethod $<-
10+ # ' @rdname SpatialData
11+ setReplaceMethod ("$ ", "SpatialData", \(x, name, value) `[[<-`(x, i=name, value=value))
12+
913# ' @export
1014# ' @rdname SpatialData
1115# ' @importFrom methods callNextMethod
@@ -16,9 +20,7 @@ setMethod("[[", c("SpatialData", "numeric"), \(x, i, ...) {
1620
1721# ' @rdname SpatialData
1822# ' @export
19- setMethod ("[[ ", c("SpatialData", "character"), \(x, i, ...) {
20- attr(x , grep(i , names(attributes(x )), value = TRUE ))
21- })
23+ setMethod ("[[ ", c("SpatialData", "character"), \(x, i, ...) attr(x, i))
2224
2325# data/meta ----
2426
@@ -142,6 +144,12 @@ setMethod("element", c("SpatialData", "missing"), \(x, i) element(x, 1))
142144setMethod ("element ", c("SpatialData", "ANY"), \(x, i)
143145 stop(" invalid 'i'; should be a string specifying an element in 'x'" ))
144146
147+ # ' @rdname SpatialData
148+ # ' @export
149+ setReplaceMethod ("element ",
150+ c(" SpatialData" , " character" ),
151+ \(x , i , value ) { x [[layer(x , i )]][[i ]] <- value ; x })
152+
145153# get all ----
146154
147155# ' @export
@@ -173,7 +181,9 @@ all <- paste0(one, "s")
173181# ' @exportMethod imageNames labelNames pointNames shapeNames tableNames
174182NULL
175183
176- f <- \(. ) setMethod(paste0(. , " Names" ), " SpatialData" , \(x ) names(x [[. ]]))
184+ f <- \(. ) setMethod(
185+ paste0(. , " Names" ), " SpatialData" ,
186+ \(x ) names(x [[paste0(. , " s" )]]))
177187for (. in one ) eval(f(. ), parent.env(environment()))
178188
179189# set nms ----
@@ -190,7 +200,7 @@ f <- \(.) setReplaceMethod(
190200 old <- names(x [[paste0(. , " s" )]])
191201 new <- names(x [[paste0(. , " s" )]]) <- value
192202 if (. == " table" ) return (x )
193- .sync_tables (x , old , new )
203+ .sync_tables_sdattrs (x , old , new )
194204 })
195205for (. in one ) eval(f(. ), parent.env(environment()))
196206
@@ -226,7 +236,7 @@ for (. in one) eval(f(.), parent.env(environment()))
226236# ' @importFrom methods setReplaceMethod
227237# ' @export
228238setReplaceMethod ("[[ ", c("SpatialData", "numeric"),
229- \(x , i , value ) { attr( x , .LAYERS [i ]) <- value ; return ( x ) })
239+ \(x , i , value ) { x [[ .LAYERS [i ]]] <- value ; x })
230240
231241# ' @rdname SpatialData
232242# ' @export
@@ -237,12 +247,17 @@ setReplaceMethod("[[", c("SpatialData", "character"),
237247 old <- names(attr(x , l ))
238248 new <- names(value )
239249 if (length(old ) == length(new ) && any(old != new ))
240- x <- .sync_tables (x , old , new )
250+ x <- .sync_tables_sdattrs (x , old , new )
241251 }
242252 attr(x , l ) <- value
243- if (l != " tables" )
253+ if (l != " tables" ) {
244254 x <- .sync_tables_on_drop(x )
245- return (x )
255+ } else {
256+ for (t in tableNames(x )) {
257+ x <- .sync_shapes_on_drop(x , t )
258+ }
259+ }
260+ x
246261 })
247262
248263# |_value=list ----
@@ -258,7 +273,7 @@ f <- \(.) setReplaceMethod(.,
258273 old <- names(attr(x , . ))
259274 new <- names(value )
260275 if (length(old ) == length(new ) && any(old != new ))
261- x <- .sync_tables (x , old , new )
276+ x <- .sync_tables_sdattrs (x , old , new )
262277 }
263278 attr(x , . ) <- value
264279 if (. != " tables" )
@@ -286,14 +301,11 @@ f <- \(.) setReplaceMethod(.,
286301 y <- attr(x , paste0(. , " s" ))
287302 y [[i ]] <- value
288303 attr(x , paste0(. , " s" )) <- y
304+ if (. == " table" ) x <- .sync_shapes_on_drop(x , i )
289305 return (x )
290306 })
291307for (. in one ) eval(f(. ), parent.env(environment()))
292308
293- # TODO: something like table(x)$cluster_id <- doesn't work atm...
294- # not sure how to get around without defining all the possible
295- # SCE replacement methods :/
296-
297309# _i=numeric ----
298310
299311# ' @name SpatialData
0 commit comments