From 55e5a28b91c6ff46445f7ad188a6b5380b07e1f5 Mon Sep 17 00:00:00 2001 From: lemna Date: Tue, 25 Oct 2016 18:06:23 +0200 Subject: [PATCH 1/3] fixed an issue with txtRound digits argument for 1 < length(digits) < number of columns: it now throws an error instead of silently recycling or throwing a warning. --- R/txtFrmt.R | 18 +++++++++++++++--- tests/testthat/test-txtFrmt.R | 4 ++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/R/txtFrmt.R b/R/txtFrmt.R index 9ecf9e4..039c61e 100644 --- a/R/txtFrmt.R +++ b/R/txtFrmt.R @@ -177,7 +177,7 @@ txtPval <- function(pvalues, #' #' @param x The value/vector/data.frame/matrix to be rounded #' @param digits The number of digits to round each element to. -#' If you provide a vector each element for corresponding columns. +#' If you provide a vector each element will apply to the corresponding columns. #' @param excl.cols Columns to exclude from the rounding procedure. #' This can be either a number or regular expression. Skipped if x is a vector. #' @param excl.rows Rows to exclude from the rounding procedure. @@ -205,7 +205,13 @@ txtRound <- function(x, ...){ #' @export #' @rdname txtRound txtRound.default = function(x, digits = 0, txt.NA = "", dec = ".", ...){ - if(length(digits) > length(x)) stop("You have ", length(digits), " but only a vector of length ", length(x), ": ", paste(x, collape=", ")) + if(length(digits) != 1 & length(digits) != length(x)) + stop("You have ", + length(digits), + " digits specifications but a vector of length ", + length(x), + ": ", + paste(x, collape=", ")) dec_str <- sprintf("^[^0-9\\%s-]*([\\-]{0,1}(([0-9]*|[0-9]+[ 0-9]+)[\\%s]|)[0-9]+)(|[^0-9]+.*)$", dec, dec) @@ -281,7 +287,13 @@ txtRound.matrix <- function(x, digits = 0, excl.cols, excl.rows, ...){ if (length(rows) == 0) stop("No rows to round") - if(length(digits) > length(cols)) stop("You have ", length(digits), " but only ", length(cols), " to apply them to: ", paste(cols, collape=", ")) + if(length(digits) != 1 & length(digits) != length(cols)) + stop("You have ", + length(digits), + " digits specifications but ", + length(cols), + " columns to apply them to: ", + paste(cols, collapse = ", ")) ret_x <- x for (row in rows){ diff --git a/tests/testthat/test-txtFrmt.R b/tests/testthat/test-txtFrmt.R index a800c46..5e36228 100644 --- a/tests/testthat/test-txtFrmt.R +++ b/tests/testthat/test-txtFrmt.R @@ -74,6 +74,10 @@ test_that("Numerical matrices",{ expect_equivalent(txtRound(matrix(c(NA, 2.22), ncol=1), 1, txt.NA = "missing")[1,1], "missing") + + expect_error(txtRound(test_mx, digits = c(2, 3, 4, 5))) + + expect_error(txtRound(test_mx, digits = c(2, 3))) }) From cf9624271a7fb99b44984d9e01451ae4eac6bc66 Mon Sep 17 00:00:00 2001 From: lemna Date: Tue, 25 Oct 2016 18:55:35 +0200 Subject: [PATCH 2/3] updated helpfile --- man/txtRound.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/txtRound.Rd b/man/txtRound.Rd index ab8d870..a28d448 100644 --- a/man/txtRound.Rd +++ b/man/txtRound.Rd @@ -21,7 +21,7 @@ txtRound(x, ...) \item{...}{Passed to next method} \item{digits}{The number of digits to round each element to. -If you provide a vector each element for corresponding columns.} +If you provide a vector each element will apply to the corresponding columns.} \item{txt.NA}{The string to exchange NA with} From 6bd391084604f3d77ff89fff5c6c25b424097c4f Mon Sep 17 00:00:00 2001 From: lemna Date: Tue, 25 Oct 2016 19:08:16 +0200 Subject: [PATCH 3/3] updating txtFrmt.R: one last mistake collape -> collapse --- R/txtFrmt.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/txtFrmt.R b/R/txtFrmt.R index 039c61e..f2f93b5 100644 --- a/R/txtFrmt.R +++ b/R/txtFrmt.R @@ -211,7 +211,7 @@ txtRound.default = function(x, digits = 0, txt.NA = "", dec = ".", ...){ " digits specifications but a vector of length ", length(x), ": ", - paste(x, collape=", ")) + paste(x, collapse=", ")) dec_str <- sprintf("^[^0-9\\%s-]*([\\-]{0,1}(([0-9]*|[0-9]+[ 0-9]+)[\\%s]|)[0-9]+)(|[^0-9]+.*)$", dec, dec)