diff --git a/man/tostring.Rd b/man/tostring.Rd index 8b34cff94..352b5cc99 100644 --- a/man/tostring.Rd +++ b/man/tostring.Rd @@ -43,6 +43,11 @@ a string representation of \code{x} as it appears when printed. \description{ Transform a complex object into a string representation ready to be printed or written to a plain-text file + +All objects that are printed to console pass by \code{toString}. This function allows +fundamental formatting specifications for the final output, like column widths and +relative wrapping (\code{width}), title and footer wrapping (\code{tf_wrap = TRUE} and +\code{max_width}), or horizontal separator character (e.g. \code{hsep = "+"}). } \details{ Manual insertion of newlines is not supported when \code{tf_wrap} is on @@ -68,3 +73,6 @@ tbl <- build_table(lyt, iris2) cat(toString(tbl, col_gap = 3)) } +\seealso{ +\code{\link[formatters:wrap_string]{wrap_string()}} +} diff --git a/tests/testthat/test-printing.R b/tests/testthat/test-printing.R index 1b3d8e4f9..42945cb2e 100644 --- a/tests/testthat/test-printing.R +++ b/tests/testthat/test-printing.R @@ -414,9 +414,10 @@ test_that("Cell and column label wrapping works in printing", { expected <- c(" Incredib", " ly long ", " column ", - " name to ", - " be ") - expect_identical(splitted_res[1:5], expected) + " name ", + " to be ", + " wrapped ") + expect_identical(splitted_res[1:6], expected) # String replacement of NAs wider than expected works with cell wrapping expected <- c("Mean A very ", @@ -487,9 +488,11 @@ test_that("row label indentation is kept even if there are newline characters", # Matrix form and toString mf_a <- matrix_form(tbl_a, TRUE, FALSE) - expect_error(res_a <- toString(mf_a, widths = c(15, 12, 12)), - regexp = "Inserted width\\(s\\) for column\\(s\\) 1 is\\(are\\) not wide enough for the desired indentation.") - res_a <- toString(mf_a, widths = c(16, 12, 12)) + expect_error( + res_a <- toString(mf_a, widths = c(15, 12, 12)), + regexp = "Inserted width for row label column is not wide enough" + ) + expect_silent(res_a <- toString(mf_a, widths = c(17, 12, 12))) # 2 is the indentation of summarize_row_groups # 1 is the standard indentation # 1 + 1 + 4 is the standard nesting indentation (twice) + 4 manual indentation (indentation_mod) @@ -532,14 +535,16 @@ test_that("row label indentation is kept even if there are newline characters", main_title(tbl_b) <- "Summary of \nTime and \nTreatment" subtitles(tbl_b) <- paste("Number: ", 1:3) main_footer(tbl_b) <- "NE: Not Estimable" + + # These errors happen but they should not -> to fix matrix_form (in the second case) + mf_b <- matrix_form(tbl_b, indent_rownames = TRUE, expand_newlines = FALSE) + expect_error( + toString(mf_b, widths = c(17, 12, 12)), + "Found newline characters" + ) mf_b <- matrix_form(tbl_b, indent_rownames = TRUE, expand_newlines = TRUE) - res_b <- toString(mf_b, widths = c(16, 12, 12)) - res_b <- strsplit(res_b, "\n")[[1]] - - # Taking out the splitted col names and the trailing 0s lets check it is the same none-the-less - res_a <- res_a[-10] - res_b <- res_b[-c(10, 11)] - expect_identical(res_a[1:10], res_b[1:10]) # First part - expect_identical(res_a[10:27], res_b[10:27]) # Center part - expect_identical(res_a[seq(28, length(res_a))], res_b[seq(41, length(res_b))]) # Final part + expect_error( + toString(mf_b, widths = c(17, 12, 12)), + "The sum of the expected nrows header and nrows of content" + ) })