diff --git a/R/combine.R b/R/combine.R index 962bccd..1bfcb4d 100644 --- a/R/combine.R +++ b/R/combine.R @@ -57,8 +57,7 @@ setMethod("combine", c("list", "missing"), \(x, y, ...) { # get current element names old <- lapply(x, \(z) unlist(colnames(z))) # get list names; if missing, use empty strings - if (is.null(nms <- names(x))) - nms <- character(length(x)) + nms <- names(x) %||% character(length(x)) # prepend list names to element names where available new <- lapply(seq_along(x), \(i) { if (nms[i] == "") return(old[[i]]) diff --git a/R/crop.R b/R/crop.R index 2b77e00..18b6b11 100644 --- a/R/crop.R +++ b/R/crop.R @@ -109,16 +109,6 @@ NULL # get transformation for space 'j' j <- .val_id(j, CTname(x)) ct <- CTlist(x)[[j]] - # helper to adapt transformation data to spatial (XY) dims - axs <- axes(x) - nms <- vapply(axs, \(.) .$name, character(1)) - ix <- match("x", nms) - iy <- match("y", nms) - if (is.na(ix) || is.na(iy)) { - # default to last two (YX) - n <- length(nms) - ix <- n; iy <- n-1 - } ax <- .get_xy_axes(x) .adapt <- \(t, type) { if (is.null(t)) return(NULL) diff --git a/R/sdArray.R b/R/sdArray.R index 937546a..041cce0 100644 --- a/R/sdArray.R +++ b/R/sdArray.R @@ -120,7 +120,8 @@ setMethod("data_type", "DelayedArray", \(x) { if (is.null(v)) return() if (v == "0.5") x <- x$ome # NOTE: can't use 'vapply' as we - # have encountered integer 'label's + # have encountered integer 'label's + # Tracked in https://github.com/scverse/spatialdata/issues/1136 x <- x$omero$channels x$label %||% unlist(lapply(x, `[[`, "label")) } diff --git a/R/sdAttrs.R b/R/sdAttrs.R index e70c4ca..ef86dae 100644 --- a/R/sdAttrs.R +++ b/R/sdAttrs.R @@ -216,8 +216,7 @@ setMethod("regions", "SingleCellExperiment", \(x) { #' @importFrom SingleCellExperiment int_metadata<- setReplaceMethod("region", c("SingleCellExperiment", "character"), \(x, value) { stopifnot(all(nchar(value) > 0, na.rm=TRUE)) - if (is.null(rk <- region_key(x))) - rk <- region_key(x) <- "region" + rk <- region_key(x) <- region_key(x) %||% "region" int_metadata(x)$spatialdata_attrs[[rk]] <- sort(unique(value)) return(x) }) @@ -237,7 +236,7 @@ setReplaceMethod("region", c("SingleCellExperiment", "NULL"), \(x, value) { setReplaceMethod("regions", c("SingleCellExperiment", "character"), \(x, value) { stopifnot(length(value) %in% c(1, ncol(x))) stopifnot(all(nchar(value) > 0, na.rm=TRUE)) - if (is.null(rk <- region_key(x))) region_key(x) <- "region" + rk <- region_key(x) <- region_key(x) %||% "region" int_metadata(x)$spatialdata_attrs[[rk]] <- sort(unique(value)) int_colData(x)[[rk]] <- value return(x) @@ -314,9 +313,7 @@ setMethod("instances", "SingleCellExperiment", \(x) { #' @rdname SpatialDataAttrs #' @importFrom SingleCellExperiment int_colData<- setReplaceMethod("instances", c("SingleCellExperiment", "ANY"), \(x, value) { - ik <- instance_key(x) - if (is.null(ik)) - ik <- "instance_id" + ik <- instance_key(x) %||% "instance_id" int_colData(x)[[ik]] <- value return(x) }) diff --git a/R/sdFrame.R b/R/sdFrame.R index c194d6a..bb69118 100644 --- a/R/sdFrame.R +++ b/R/sdFrame.R @@ -77,7 +77,7 @@ NULL #' @importFrom sf st_sf st_sfc st_as_sf st_point st_polygon .df_to_sf <- \(data, type=c("POINT", "POLYGON")) { type <- match.arg(type) - if (is.null(data) || isTRUE(nrow(data) == 0)) { + if (isTRUE(NROW(data) == 0)) { # return empty data.frame with geometry column fn <- switch(type, POINT=st_point, st_polygon) return(st_sf(geometry=st_sfc(fn())[0], crs=NA)) @@ -134,8 +134,7 @@ SpatialDataPoint <- \(data=NULL, meta=SpatialDataAttrs(type="frame"), metadata=l if (!is(data, "duckspatial_df")) data <- .duck(data, "sdPoint") za <- as.list(meta) - if (is.null(za$spatialdata_attrs)) - za$spatialdata_attrs <- list() + za$spatialdata_attrs <- za$spatialdata_attrs %||% list() if (!is.null(ik)) { stopifnot(ik %in% colnames(data)) instance_key(za) <- ik diff --git a/R/tables.R b/R/tables.R index 8390ccd..cb3035e 100644 --- a/R/tables.R +++ b/R/tables.R @@ -182,8 +182,8 @@ setMethod("setTable", c("SpatialData", "character"), \(x, i, y, if (!. %in% c("labels", "shapes")) stop("can't add 'table' for", .) - if (is.null(region_key(y))) region_key(y) <- rk - if (is.null(instance_key(y))) instance_key(y) <- ik + region_key(y) <- region_key(y) %||% rk + instance_key(y) <- instance_key(y) %||% ik if (is.null(region(y))) { regions(y) <- i