From d9b6876c1123a81cf8b7455930baec056346b5c0 Mon Sep 17 00:00:00 2001 From: Brandon Bertelsen Date: Thu, 12 May 2022 17:03:53 -0400 Subject: [PATCH 1/2] Calculate tab insert before rounding occurs --- R/reformatResults.R | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/R/reformatResults.R b/R/reformatResults.R index 738edbd..7c3c73b 100644 --- a/R/reformatResults.R +++ b/R/reformatResults.R @@ -205,6 +205,13 @@ reformatVar <- function(var, banner_name, theme, proportions, banner_info, latex should_round <- ifelse(var$alias %in% theme$latex_round_percentages_exception, !should_round, should_round ) + + # Calculate tabInsertions before rounding! + if (var$type %in% c("categorical", "categorical_array") && dt %in% "body" && + any(var$inserts %in% c("Heading", "Subtotal"))) { + data <- as.matrix(calcTabInsertions(data, var$inserts_obj, var$categories)) + } + if (should_round & dt != "weighted_n") { data[] <- apply(data, 2, roundPropCategorical, theme$digits) } else if (!is.null(rdig) && !is.infinite(rdig)) { @@ -232,10 +239,6 @@ reformatVar <- function(var, banner_name, theme, proportions, banner_info, latex dimnames = list(c(theme$format_totals_row$name), colnames(data)) ) } - if (var$type %in% c("categorical", "categorical_array") && dt %in% "body" && - any(var$inserts %in% c("Heading", "Subtotal"))) { - data <- as.matrix(calcTabInsertions(data, var$inserts_obj, var$categories)) - } if (weight_v && nrow(data) > 1) { data <- rbind(apply(data, 2, min, na.rm = TRUE), apply(data, 2, max, na.rm = TRUE)) From 732e348bbca0674c1fd73a6add951f7787673308 Mon Sep 17 00:00:00 2001 From: Brandon Bertelsen Date: Thu, 12 May 2022 18:15:14 -0400 Subject: [PATCH 2/2] Updating tests for new rounding --- tests/testthat/ref/tabbook1.tex | 10 +++++----- tests/testthat/ref/topline1.tex | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/testthat/ref/tabbook1.tex b/tests/testthat/ref/tabbook1.tex index 5a161b0..1075641 100644 --- a/tests/testthat/ref/tabbook1.tex +++ b/tests/testthat/ref/tabbook1.tex @@ -154,9 +154,9 @@ \textbf{header} & & & & & \\ Cat & 30\% & 38\% & 25\% & 18\% & 44\% \\ -\textbf{Net: Cat/Bird} & \textbf{50\%} & \textbf{38\%} & \textbf{58\%} & \textbf{30\%} & \textbf{73\%} \\ +\textbf{Net: Cat/Bird} & \textbf{50\%} & \textbf{38\%} & \textbf{58\%} & \textbf{31\%} & \textbf{73\%} \\ Dog & 50\% & 62\% & 42\% & 69\% & 27\% \\ -\textbf{Net: Cat/Dog} & \textbf{80\%} & \textbf{100\%} & \textbf{67\%} & \textbf{87\%} & \textbf{71\%} \\ +\textbf{Net: Cat/Dog} & \textbf{80\%} & \textbf{100\%} & \textbf{67\%} & \textbf{88\%} & \textbf{71\%} \\ Bird & 20\% & 0\% & 33\% & 12\% & 29\% \\ \midrule Totals & 100\% & 100\% & 100\% & 99\% & 100\% \\ @@ -200,7 +200,7 @@ Cat & 49\% & 22\% & 66\% & 100\% & 26\% \\ Dog & 43\% & 56\% & 34\% & 0\% & 62\% \\ Bird & 9\% & 22\% & 0\% & 0\% & 13\% \\ -\textbf{Net: Cat/Dog} & \textbf{92\%} & \textbf{78\%} & \textbf{100\%} & \textbf{100\%} & \textbf{88\%} \\ +\textbf{Net: Cat/Dog} & \textbf{91\%} & \textbf{78\%} & \textbf{100\%} & \textbf{100\%} & \textbf{87\%} \\ \midrule Totals & 101\% & 100\% & 100\% & 100\% & 101\% \\ Unweighted N & \multicolumn{1}{c}{11} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{8} & \multicolumn{1}{c}{5} & \multicolumn{1}{c}{6} \\ @@ -218,7 +218,7 @@ Cat & 49\% & 22\% & 100\% & 46\% \\ Dog & 43\% & 56\% & 0\% & 54\% \\ Bird & 9\% & 22\% & 0\% & 0\% \\ -\textbf{Net: Cat/Dog} & \textbf{92\%} & \textbf{78\%} & \textbf{100\%} & \textbf{100\%} \\ +\textbf{Net: Cat/Dog} & \textbf{91\%} & \textbf{78\%} & \textbf{100\%} & \textbf{100\%} \\ \midrule Totals & 101\% & 100\% & 100\% & 100\% \\ Unweighted N & \multicolumn{1}{c}{11} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{6} \\ @@ -241,7 +241,7 @@ Cat & 42\% & 50\% & 36\% & 54\% & 18\% \\ Dog & 37\% & 14\% & 54\% & 46\% & 18\% \\ Bird & 21\% & 36\% & 11\% & 0\% & 64\% \\ -\textbf{Net: Cat/Dog} & \textbf{79\%} & \textbf{64\%} & \textbf{90\%} & \textbf{100\%} & \textbf{36\%} \\ +\textbf{Net: Cat/Dog} & \textbf{79\%} & \textbf{64\%} & \textbf{89\%} & \textbf{100\%} & \textbf{36\%} \\ \midrule Totals & 100\% & 100\% & 101\% & 100\% & 100\% \\ Unweighted N & \multicolumn{1}{c}{16} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{12} & \multicolumn{1}{c}{9} & \multicolumn{1}{c}{7} \\ diff --git a/tests/testthat/ref/topline1.tex b/tests/testthat/ref/topline1.tex index f82f76c..1e54e02 100644 --- a/tests/testthat/ref/topline1.tex +++ b/tests/testthat/ref/topline1.tex @@ -118,7 +118,7 @@ \endfoot \endlastfoot -& Home & 49\% & 43\% & 9\% & \textbf{92\%} \\ +& Home & 49\% & 43\% & 9\% & \textbf{91\%} \\ & Work & 42\% & 37\% & 21\% & \textbf{79\%} \\ \end{longtable}