From c11276fecaaaf2ad41f8ce86cf1c40247ccc8825 Mon Sep 17 00:00:00 2001 From: HediaTnani Date: Fri, 12 Jan 2024 16:47:30 -0500 Subject: [PATCH] Validate qsvPCs is a prcomp object --- R/get_qsvs.R | 17 +++++++++++------ tests/testthat/test-get_qsvs.R | 4 ++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/R/get_qsvs.R b/R/get_qsvs.R index d1e767e..42088d5 100644 --- a/R/get_qsvs.R +++ b/R/get_qsvs.R @@ -16,11 +16,16 @@ #' get_qsvs(qsv, 2) get_qsvs <- function(qsvPCs, k) { # check that k isn't zero - if (k <= 0 | k > ncol(qsvPCs$x)) { - stop(paste("k must between 1 and",ncol(qsvPCs$x))) - } + if (k <= 0 | k > ncol(qsvPCs$x)) { + stop(paste("k must between 1 and",ncol(qsvPCs$x))) + } - qSVs <- qsvPCs$x[, seq_len(k), drop = FALSE] - colnames(qSVs) <- paste0("qSV", seq_len(k)) - return(qSVs) + # Validate qsvPCs is a prcomp object + if (!is(qsvPCs, "prcomp")) { + stop("qsvPCs must be a prcomp object.", call. = FALSE) + } + + qSVs <- qsvPCs$x[, seq_len(k), drop = FALSE] + colnames(qSVs) <- paste0("qSV", seq_len(k)) + return(qSVs) } diff --git a/tests/testthat/test-get_qsvs.R b/tests/testthat/test-get_qsvs.R index d785a26..e9f39ab 100644 --- a/tests/testthat/test-get_qsvs.R +++ b/tests/testthat/test-get_qsvs.R @@ -29,4 +29,8 @@ test_that("k is 0 throws an error", { test_that("k is higher than the number of columns throws an error", { k = ncol(qsv$x) + 1000 expect_error(get_qsvs(qsv, k), paste("k must between 1 and",ncol(qsv$x))) +}) + +test_that("input has to be a prcomp", { + expect_error(get_qsvs(covComb_tx_deg, 3), "qsvPCs must be a prcomp object.") }) \ No newline at end of file