Skip to content

Commit 5ed6f06

Browse files
committed
add numeric check for user-provided columns
1 parent 25a45e7 commit 5ed6f06

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

R/plot-pgs.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ create.pgs.density.plot <- function(
142142
if (!all(pgs.columns %in% colnames(pgs.data))) {
143143
stop('pgs.columns must be a subset of the column names in pgs.data, please check your input');
144144
}
145+
# If user-provided pgs columns, check that they are numeric
146+
if (!all(sapply(pgs.data[, pgs.columns, drop = FALSE], is.numeric))) {
147+
stop('All columns specified in pgs.columns must be numeric.');
148+
}
145149
} else {
146150
# If no pgs columns provided, default to recognized PGS columns
147151
recognized.pgs.colnames <- c('PGS', 'PGS.with.replaced.missing', 'PGS.with.normalized.missing');
@@ -480,6 +484,10 @@ create.pgs.boxplot <- function(
480484
if (!all(pgs.columns %in% colnames(pgs.data))) {
481485
stop('pgs.columns must be a subset of the column names in pgs.data, please check your input');
482486
}
487+
# If user-provided pgs columns, check that they are numeric
488+
if (!all(sapply(pgs.data[, pgs.columns, drop = FALSE], is.numeric))) {
489+
stop('All columns specified in pgs.columns must be numeric.');
490+
}
483491
} else {
484492
# If no pgs columns provided, default to recognized PGS columns
485493
recognized.pgs.colnames <- c('PGS', 'PGS.with.replaced.missing', 'PGS.with.normalized.missing');
@@ -796,6 +804,10 @@ create.pgs.with.continuous.phenotype.plot <- function(
796804
if (!all(pgs.columns %in% colnames(pgs.data))) {
797805
stop('pgs.columns must be a subset of the column names in pgs.data, please check your input');
798806
}
807+
# If user-provided pgs columns, check that they are numeric
808+
if (!all(sapply(pgs.data[, pgs.columns, drop = FALSE], is.numeric))) {
809+
stop('All columns specified in pgs.columns must be numeric.');
810+
}
799811
} else {
800812
# If no pgs columns provided, default to recognized PGS columns
801813
recognized.pgs.colnames <- c('PGS', 'PGS.with.replaced.missing', 'PGS.with.normalized.missing');

tests/testthat/test-plotting.R

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# plotting functions take a long time to run, this var toggles off plotting tests for faster testing
22
SKIP.PLOTS <- FALSE;
3-
SKIP.COMPREHENSIVE.CASES <- FALSE;
3+
SKIP.COMPREHENSIVE.CASES <- TRUE#FALSE;
44
skip.plotting.tests <- function(skip.plots = FALSE) {
55
if (skip.plots) {
66
skip('Plotting tests disabled');
@@ -90,6 +90,15 @@ test_that(
9090
),
9191
'pgs.columns must be a subset of the column names in pgs.data, please check your input'
9292
);
93+
# check that user-provided pgs.columns are numeric
94+
pgs.test$non.numeric.column <- as.character(pgs.test$PGS); # create a non-numeric column
95+
expect_error(
96+
create.pgs.density.plot(
97+
pgs.data = pgs.test,
98+
pgs.columns = c('non.numeric.column')
99+
),
100+
'All columns specified in pgs.columns must be numeric'
101+
);
93102
}
94103
);
95104

@@ -310,6 +319,17 @@ test_that(
310319
'pgs.columns must be a subset of the column names in pgs.data, please check your input'
311320
);
312321

322+
# check that user-provided pgs.columns are numeric
323+
pgs.test$non.numeric.column <- as.character(pgs.test$PGS); # create a non-numeric column
324+
expect_error(
325+
create.pgs.with.continuous.phenotype.plot(
326+
pgs.data = pgs.test,
327+
phenotype.columns = 'continuous.phenotype',
328+
pgs.columns = c('non.numeric.column')
329+
),
330+
'All columns specified in pgs.columns must be numeric'
331+
);
332+
313333
# check that phenotype.column is a character vector
314334
expect_error(
315335
create.pgs.with.continuous.phenotype.plot(
@@ -929,6 +949,15 @@ test_that(
929949
),
930950
'pgs.columns must be a subset of the column names in pgs.data, please check your input'
931951
);
952+
# check that user-provided pgs.columns are numeric
953+
pgs.test$non.numeric.column <- as.character(pgs.test$PGS); # create a non-numeric column
954+
expect_error(
955+
create.pgs.boxplot(
956+
pgs.data = pgs.test,
957+
pgs.columns = c('non.numeric.column')
958+
),
959+
'All columns specified in pgs.columns must be numeric'
960+
);
932961
}
933962
);
934963

0 commit comments

Comments
 (0)