Skip to content

Commit 15506f4

Browse files
authored
Merge pull request #17 from lemna/master
proposal to fix issue #16
2 parents 036bd87 + 6bd3910 commit 15506f4

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

R/txtFrmt.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ txtPval <- function(pvalues,
177177
#'
178178
#' @param x The value/vector/data.frame/matrix to be rounded
179179
#' @param digits The number of digits to round each element to.
180-
#' If you provide a vector each element for corresponding columns.
180+
#' If you provide a vector each element will apply to the corresponding columns.
181181
#' @param excl.cols Columns to exclude from the rounding procedure.
182182
#' This can be either a number or regular expression. Skipped if x is a vector.
183183
#' @param excl.rows Rows to exclude from the rounding procedure.
@@ -205,7 +205,13 @@ txtRound <- function(x, ...){
205205
#' @export
206206
#' @rdname txtRound
207207
txtRound.default = function(x, digits = 0, txt.NA = "", dec = ".", ...){
208-
if(length(digits) > length(x)) stop("You have ", length(digits), " but only a vector of length ", length(x), ": ", paste(x, collape=", "))
208+
if(length(digits) != 1 & length(digits) != length(x))
209+
stop("You have ",
210+
length(digits),
211+
" digits specifications but a vector of length ",
212+
length(x),
213+
": ",
214+
paste(x, collapse=", "))
209215

210216
dec_str <- sprintf("^[^0-9\\%s-]*([\\-]{0,1}(([0-9]*|[0-9]+[ 0-9]+)[\\%s]|)[0-9]+)(|[^0-9]+.*)$",
211217
dec, dec)
@@ -281,7 +287,13 @@ txtRound.matrix <- function(x, digits = 0, excl.cols, excl.rows, ...){
281287
if (length(rows) == 0)
282288
stop("No rows to round")
283289

284-
if(length(digits) > length(cols)) stop("You have ", length(digits), " but only ", length(cols), " to apply them to: ", paste(cols, collape=", "))
290+
if(length(digits) != 1 & length(digits) != length(cols))
291+
stop("You have ",
292+
length(digits),
293+
" digits specifications but ",
294+
length(cols),
295+
" columns to apply them to: ",
296+
paste(cols, collapse = ", "))
285297

286298
ret_x <- x
287299
for (row in rows){

man/txtRound.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-txtFrmt.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ test_that("Numerical matrices",{
7474

7575
expect_equivalent(txtRound(matrix(c(NA, 2.22), ncol=1), 1, txt.NA = "missing")[1,1],
7676
"missing")
77+
78+
expect_error(txtRound(test_mx, digits = c(2, 3, 4, 5)))
79+
80+
expect_error(txtRound(test_mx, digits = c(2, 3)))
7781
})
7882

7983

0 commit comments

Comments
 (0)