Skip to content

Commit 67e62ef

Browse files
committed
refactor: use utility function for ensuring class consistency
1 parent b3c6f69 commit 67e62ef

3 files changed

Lines changed: 20 additions & 10 deletions

File tree

R/ds.dim.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ ds.dim <- function(x=NULL, type='both', datasources=NULL) {
103103
cally <- call("dimDS", x)
104104
results <- DSI::datashield.aggregate(datasources, cally)
105105

106-
# check class consistency across studies
107-
classes <- lapply(results, function(r) r$class)
108-
if(length(unique(lapply(classes, sort))) > 1){
109-
stop("The input object is not of the same class in all studies!", call.=FALSE)
110-
}
106+
.checkClassConsistency(results)
111107

112108
# extract dimensions from results
113109
dimensions <- lapply(results, function(r) r$dim)

R/ds.length.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,7 @@ ds.length <- function(x=NULL, type='both', datasources=NULL){
9696
cally <- call("lengthDS", x)
9797
results <- DSI::datashield.aggregate(datasources, cally)
9898

99-
# check class consistency across studies
100-
classes <- lapply(results, function(r) r$class)
101-
if(length(unique(lapply(classes, sort))) > 1){
102-
stop("The input object is not of the same class in all studies!", call.=FALSE)
103-
}
99+
.checkClassConsistency(results)
104100

105101
# extract lengths from results
106102
lengths <- lapply(results, function(r) r$length)

R/utils.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@
3636
return(datasources)
3737
}
3838

39+
#' Check cross-study class consistency from a list of server aggregate results
40+
#'
41+
#' Batch-refactored server functions return a list per study that includes a
42+
#' `class` field. This helper verifies that the class field is identical across
43+
#' all studies and aborts if not.
44+
#'
45+
#' @param results A named list of server-side aggregate results, one per study,
46+
#' each containing a `class` element.
47+
#' @importFrom cli cli_abort
48+
#' @return Invisibly returns `NULL`. Called for its side effect (error checking).
49+
#' @noRd
50+
.checkClassConsistency <- function(results) {
51+
classes <- lapply(results, function(r) r$class)
52+
if (length(unique(lapply(classes, sort))) > 1) {
53+
cli_abort("The input object is not of the same class in all studies!")
54+
}
55+
}
56+
3957
#' Check That a Data Frame Name Is Provided
4058
#'
4159
#' Internal helper that checks whether a data frame or matrix object

0 commit comments

Comments
 (0)