Skip to content

Commit c20ca47

Browse files
committed
factor out function to check validity of index/identifier
1 parent a725257 commit c20ca47

5 files changed

Lines changed: 23 additions & 32 deletions

File tree

R/CTutils.R

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,7 @@ setMethod("CTlist", "SpatialDataAttrs", \(x, ...) {
7777
#' @rdname CTutils
7878
#' @export
7979
setMethod("CTdata", "SpatialDataAttrs", \(x, i=1, ...) {
80-
stopifnot(length(i) == 1)
81-
if (is.character(i)) {
82-
match.arg(i, CTname(x))
83-
i <- match(i, CTname(x))
84-
} else if (is.numeric(i)) {
85-
stopifnot(
86-
i == round(i),
87-
i %in% seq_along(CTlist(x)))
88-
} else stop("Invalid 'i'; should be a scalar character or integer")
80+
i <- .resolve_id(i, CTname(x))
8981
t <- CTtype(x)[i]
9082
if (t != "sequence")
9183
return(CTlist(x)[[i]][[t]])

R/mask.R

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,7 @@ setMethod("mask", c("SpatialData", "ANY", "ANY"), \(x, i, j, k,
5656
if (!length(ct)) stop(
5757
"can't mask; found no common ",
5858
"coordinates between 'i' and 'j'")
59-
if (missing(k)) {
60-
k <- 1
61-
} else {
62-
if (is.character(k)) {
63-
k <- match.arg(k, ct)
64-
k <- match(k, ct)
65-
} else if (is.numeric(k)) {
66-
stopifnot(k > 0, k <= length(ct))
67-
}
68-
}
59+
k <- if (missing(k)) 1 else .resolve_id(k, ct)
6960
.i <- transform(.i, ct[k])
7061
.j <- transform(.j, ct[k])
7162
t <- tryCatch(error=\(.) NULL, getTable(x, i))

R/read.R

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,13 @@ readSpatialData <- function(x,
136136

137137
# helper for layer reading
138138
.readLayer <- \(l) {
139+
# 'j' are the paths on disk, 'nms' are their basenames
139140
j <- list.dirs(file.path(x, l), recursive=FALSE, full.names=TRUE)
140-
names(j) <- basename(j)
141+
nms <- names(j) <- basename(j)
141142
opt <- args[[l]]
142143
if (!isTRUE(opt)) {
143-
if (is.numeric(opt) && opt > (. <- length(j)))
144-
stop("'", l, "=", opt, "', but only ", ., " elements found")
145-
if (is.character(opt) && length(. <- setdiff(opt, basename(j))))
146-
stop("couldn't find ", l, " of name", .)
147-
j <- j[opt]
144+
# validate each requested element
145+
j <- j[vapply(opt, .resolve_id, integer(1), ok=nms, nm=l)]
148146
}
149147
f <- paste0("read", toupper(substr(l, 1, 1)), substr(l, 2, nchar(l)-1))
150148
lapply(j, f)

R/trans.R

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,7 @@ NULL
5252
#' @rdname trans
5353
#' @importFrom BiocGenerics transform
5454
setMethod("transform", "SpatialDataElement", \(x, i=1, ...) {
55-
stopifnot(
56-
length(i) == 1, is.character(i) |
57-
(is.numeric(i) && i == round(i)))
58-
if (is.character(i)) {
59-
i <- match.arg(i, CTname(x))
60-
i <- match(i, CTname(x))
61-
}
55+
i <- .resolve_id(i, CTname(x))
6256
f <- CTtype(x)[i]
6357
t <- CTdata(x, i)
6458
if (f == "sequence") {

R/utils.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@
1010
.GlobalEnv[[nm]]
1111
}
1212

13+
# internal helper to resolve name/index to integer index
14+
.resolve_id <- \(i, ok, nm=deparse1(substitute(i))) {
15+
nm <- sprintf("'%s'", nm)
16+
if (is.character(i)) {
17+
i <- match.arg(i, ok)
18+
return(match(i, ok))
19+
}
20+
if (is.numeric(i) && i == round(i) && length(i) == 1) {
21+
if (i < 1 || i > length(ok)) {
22+
stop(sprintf("invalid %s index: %d (max: %d)", nm, i, length(ok)))
23+
}
24+
return(as.integer(i))
25+
}
26+
stop(sprintf("invalid %s; expected character or integer index", nm))
27+
}
28+
1329
# internal helper for null-coalescing
1430
`%||%` <- \(a, b) if (is.null(a)) b else a
1531

0 commit comments

Comments
 (0)