diff --git a/R/format_value.R b/R/format_value.R index 0dde671ab..95637583f 100644 --- a/R/format_value.R +++ b/R/format_value.R @@ -19,7 +19,7 @@ call_format_fun <- function(f, formats_1d <- c( "xx", "xx.", "xx.x", "xx.xx", "xx.xxx", "xx.xxxx", "xx%", "xx.%", "xx.x%", "xx.xx%", "xx.xxx%", "(N=xx)", "N=xx", ">999.9", ">999.99", - "x.xxxx | (<0.0001)" + "x.xxxx | (<0.0001)", "none", " " ) formats_2d <- c( @@ -356,13 +356,15 @@ format_value <- function(x, format = NULL, output = c("ascii", "html"), na_str = "'. Run `list_valid_format_labels()` to get a list of all available formats." ) } - if (format != "xx" && length(x) != l) { + if (!(format %in% c("xx", " ", "none")) && length(x) != l) { stop( "Cell contents <", paste(x, collapse = ", "), "> and format '", format, "' are of different lengths (", length(x), " vs ", l, ")." ) } switch(format, + "none" = as.character(x), + " " = as.character(x), "xx" = as.character(x), "xx." = round_fmt(x, digits = 0, na_str = na_str, round_type = round_type), "xx.x" = round_fmt(x, digits = 1, na_str = na_str, round_type = round_type), diff --git a/tests/testthat/test-formatters.R b/tests/testthat/test-formatters.R index 76b7d8764..30a591fe3 100644 --- a/tests/testthat/test-formatters.R +++ b/tests/testthat/test-formatters.R @@ -47,6 +47,16 @@ test_that("formats work", { paste(values[1]) ) + expect_identical( + format_value(values, format = " "), + paste(values) + ) + + expect_identical( + format_value(values, format = "none"), + paste(values) + ) + expect_identical( format_value(values[1], format = "xx."), "5" @@ -386,6 +396,14 @@ test_that("formats work", { format_value(NA, "xx", na_str = "-"), "-" ) + expect_equal( + format_value(NA, " ", na_str = "-"), + "-" + ) + expect_equal( + format_value(NA, "none", na_str = "-"), + "-" + ) expect_equal( format_value(c(1, NA), "xx"), @@ -877,7 +895,9 @@ test_that("All supported 1d format cases of decimal alignment", { "m N=11 N=11 right ", "n >999.9 >999.9 dec_left ", "o >999.99 >999.99 dec_left ", - "p 1.1111 | (<0.0001) 1.1111 | (<0.0001) right " + "p 1.1111 | (<0.0001) 1.1111 | (<0.0001) right ", + "q none none left ", + "r dec_right" ) expect_identical(res_dec, expected) })