|
17 | 17 | #' imageNames(y) |
18 | 18 | #' region(table(y, 1)) |
19 | 19 | #' region(table(y, 2)) |
20 | | -#' |
21 | | -#' @importFrom BiocGenerics combine |
| 20 | +NULL |
| 21 | + |
22 | 22 | #' @export |
23 | | -setMethod("combine", |
24 | | - c("SpatialData", "SpatialData"), |
25 | | - \(x, y, ...) { |
26 | | - # ensure element names are unique across objects |
27 | | - old <- list(unlist(colnames(x)), unlist(colnames(y))) |
28 | | - idx <- rep.int(c(1, 2), vapply(old, length, integer(1))) |
29 | | - new <- split(make.unique(unlist(old)), idx) |
30 | | - for (i in c(1, 2)) { |
31 | | - z <- get(c("x", "y")[i]) |
32 | | - layer_nms <- setdiff(rownames(z), "tables") |
33 | | - old_nms <- unlist(colnames(z)[layer_nms]) |
34 | | - # find new names for these elements |
35 | | - j <- match(old_nms, old[[i]]) |
36 | | - new_nms <- new[[i]][j] |
37 | | - |
38 | | - # rename elements |
39 | | - for (l in layer_nms) { |
40 | | - j <- match(names(z[[l]]), old[[i]]) |
41 | | - names(z[[l]]) <- new[[i]][j] |
42 | | - } |
43 | | - # sync tables |
44 | | - z <- .sync_tables(z, old_nms, new_nms) |
45 | | - |
46 | | - # rename tables themselves |
47 | | - j <- match(tableNames(z), old[[i]]) |
48 | | - tableNames(z) <- new[[i]][j] |
49 | | - |
50 | | - assign(c("x", "y")[i], z) |
| 23 | +#' @rdname combine |
| 24 | +#' @importFrom BiocGenerics combine |
| 25 | +setMethod("combine", c("SpatialData", "SpatialData"), \(x, y, ...) { |
| 26 | + # ensure element names are unique across objects |
| 27 | + old <- list(unlist(colnames(x)), unlist(colnames(y))) |
| 28 | + idx <- rep.int(c(1, 2), vapply(old, length, integer(1))) |
| 29 | + new <- split(make.unique(unlist(old)), idx) |
| 30 | + for (i in c(1, 2)) { |
| 31 | + z <- get(c("x", "y")[i]) |
| 32 | + layer_nms <- setdiff(rownames(z), "tables") |
| 33 | + old_nms <- unlist(colnames(z)[layer_nms]) |
| 34 | + # find new names for these elements |
| 35 | + j <- match(old_nms, old[[i]]) |
| 36 | + new_nms <- new[[i]][j] |
| 37 | + |
| 38 | + # rename elements |
| 39 | + for (l in layer_nms) { |
| 40 | + j <- match(names(z[[l]]), old[[i]]) |
| 41 | + names(z[[l]]) <- new[[i]][j] |
51 | 42 | } |
52 | | - SpatialData( |
53 | | - images=c(x$images, y$images), |
54 | | - labels=c(x$labels, y$labels), |
55 | | - points=c(x$points, y$points), |
56 | | - shapes=c(x$shapes, y$shapes), |
57 | | - tables=c(x$tables, y$tables)) |
58 | | - }) |
| 43 | + # sync tables |
| 44 | + z <- .sync_tables(z, old_nms, new_nms) |
| 45 | + |
| 46 | + # rename tables themselves |
| 47 | + j <- match(tableNames(z), old[[i]]) |
| 48 | + tableNames(z) <- new[[i]][j] |
| 49 | + |
| 50 | + assign(c("x", "y")[i], z) |
| 51 | + } |
| 52 | + SpatialData( |
| 53 | + images=c(x$images, y$images), |
| 54 | + labels=c(x$labels, y$labels), |
| 55 | + points=c(x$points, y$points), |
| 56 | + shapes=c(x$shapes, y$shapes), |
| 57 | + tables=c(x$tables, y$tables)) |
| 58 | +}) |
0 commit comments