From 60cb4d3a75a9b25747ed77083ec6424cf241cd9c Mon Sep 17 00:00:00 2001 From: elidaniels Date: Mon, 2 Sep 2024 14:01:25 +0200 Subject: [PATCH] feat : add include.table_count param to proc_freq --- DESCRIPTION | 3 ++- NEWS.md | 1 + R/proc_freq.R | 23 +++++++++++++++++------ man/flextable-package.Rd | 1 + man/proc_freq.Rd | 7 +++++-- tests/testthat/test-proc-freq.R | 27 +++++++++++++++++++++++++++ 6 files changed, 53 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a9012a3a..86bd2bdb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,11 +1,12 @@ Type: Package Package: flextable Title: Functions for Tabular Reporting -Version: 0.9.7.008 +Version: 0.9.7.009 Authors@R: c( person("David", "Gohel", , "david.gohel@ardata.fr", role = c("aut", "cre")), person("ArData", role = "cph"), person("Clementine", "Jager", role = "ctb"), + person("Eli", "Daniels", role = "ctb"), person("Panagiotis", "Skintzos", , "panagiotis.skintzos@ardata.fr", role = "aut"), person("Quentin", "Fazilleau", role = "ctb"), person("Maxim", "Nazarov", role = "ctb", diff --git a/NEWS.md b/NEWS.md index f6e5d65f..940b6a15 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,7 @@ rmarkdown (issue #632) - fix right outer border issue in grid format (issue #650) - fix `flextable_to_rmd()` issue with images in pdf (issue #651) - fix `flextable_to_rmd()` issue with local chunk `eval` option (issue #631) +- `proc_freq` can now display only the table percentages without the count using `include.table_count = FALSE`. # flextable 0.9.6 diff --git a/R/proc_freq.R b/R/proc_freq.R index 679c4d81..c698d2a0 100644 --- a/R/proc_freq.R +++ b/R/proc_freq.R @@ -117,8 +117,8 @@ relayout_freq_data <- function(x, order_by) { #' @title Frequency table #' -#' @description This function compute a one or two way -#' contingency table and create a flextable from the result. +#' @description This function computes a one or two way +#' contingency table and creates a flextable from the result. #' #' The function is largely inspired by "PROC FREQ" from "SAS" #' and was written with the intent to make it @@ -129,6 +129,7 @@ relayout_freq_data <- function(x, order_by) { #' @param include.row_percent `boolean` whether to include the row percents; defaults to `TRUE` #' @param include.column_percent `boolean` whether to include the column percents; defaults to `TRUE` #' @param include.table_percent `boolean` whether to include the table percents; defaults to `TRUE` +#' @param include.table_count `boolean` whether to include the table counts; defaults to `TRUE` #' @param weight `character` column name for weight #' @param ... unused arguments #' @importFrom stats as.formula @@ -137,8 +138,10 @@ relayout_freq_data <- function(x, order_by) { #' proc_freq(mtcars, "gear", "vs", weight = "wt") #' @export proc_freq <- function(x, row = character(), col = character(), - include.row_percent = TRUE, include.column_percent = TRUE, + include.row_percent = TRUE, + include.column_percent = TRUE, include.table_percent = TRUE, + include.table_count = TRUE, weight = character(), ...) { if (length(row) && !is.factor(x[[row]])) { x[[row]] <- as.factor(x[[row]]) @@ -178,15 +181,23 @@ proc_freq <- function(x, row = character(), col = character(), first_vline <- 1 } + table_label <- "Count" + if (!include.table_count && include.table_percent) { + table_label <- "Percent" + } else if (!include.table_count && !include.table_percent) { + stop("At least one of the include.table_* parameters must be TRUE.") + } tab <- tabulator( x = dat, rows = rows_set, columns = c(".coltitle.", col), stat = as_paragraph( - if (include.table_percent) { + if (include.table_count & include.table_percent) { as_chunk(fmt_n_percent(count, pct)) - } else { + } else if (include.table_count) { as_chunk(count, formatter = fmt_int) + } else if (include.table_percent) { + as_chunk(fmt_pct(pct)) }, as_chunk(fmt_freq_table(pct_col, pct_row, include.column_percent = include.column_percent, @@ -199,7 +210,7 @@ proc_freq <- function(x, row = character(), col = character(), if (include.column_percent || include.row_percent) { ft <- labelizor( x = ft, - labels = c(.what. = "", count = "Count", "mpct" = margins_label), j = ".what." + labels = c(.what. = "", "count" = table_label, "mpct" = margins_label), j = ".what." ) if (!is.na(fnote_lab)) { ft <- footnote(ft, diff --git a/man/flextable-package.Rd b/man/flextable-package.Rd index ae6f6687..5f3de4d7 100644 --- a/man/flextable-package.Rd +++ b/man/flextable-package.Rd @@ -51,6 +51,7 @@ Other contributors: \item Rémi Thériault (\href{https://orcid.org/0000-0003-4315-6788}{ORCID}) (theme_apa) [contributor] \item Samuel Jobert (work on pagination) [contributor] \item Keith Newman [contributor] + \item Eli Daniels [contributor] } } diff --git a/man/proc_freq.Rd b/man/proc_freq.Rd index 016425f6..d08f01f8 100644 --- a/man/proc_freq.Rd +++ b/man/proc_freq.Rd @@ -11,6 +11,7 @@ proc_freq( include.row_percent = TRUE, include.column_percent = TRUE, include.table_percent = TRUE, + include.table_count = TRUE, weight = character(), ... ) @@ -28,13 +29,15 @@ proc_freq( \item{include.table_percent}{\code{boolean} whether to include the table percents; defaults to \code{TRUE}} +\item{include.table_count}{\code{boolean} whether to include the table counts; defaults to \code{TRUE}} + \item{weight}{\code{character} column name for weight} \item{...}{unused arguments} } \description{ -This function compute a one or two way -contingency table and create a flextable from the result. +This function computes a one or two way +contingency table and creates a flextable from the result. The function is largely inspired by "PROC FREQ" from "SAS" and was written with the intent to make it diff --git a/tests/testthat/test-proc-freq.R b/tests/testthat/test-proc-freq.R index 6f8183f5..9911d6fa 100644 --- a/tests/testthat/test-proc-freq.R +++ b/tests/testthat/test-proc-freq.R @@ -42,6 +42,20 @@ count_only_dump_txt <- "" ) +no_count_full_dump_txt <- + c( + "o", "", "p", "p", "p", "p", "o", "", "No", "Yes", "Missing", "Total", "No", + "Percent", "96.4%", "", "0.2%", "", "0.0%", "", "96.7%", "", "No", "Mar. pct", + " (1)", "", "97.4% ; 99.8%", "", "22.1% ; 0.2%", "", "50.0% ; 0.0%", "", "", + "Yes", "Percent", "2.5%", "", "0.8%", "", "", "", "3.3%", "", "Yes", "Mar. pct", + "", "2.5% ; 75.7%", "", "77.9% ; 24.3%", "", "", "", "", "Missing", "Percent", + "0.0%", "", "", "", "0.0%", "", "0.0%", "", "Missing", "Mar. pct", "", "0.0% ; 50.0%", + "", "", "", "50.0% ; 50.0%", "", "", "Total", "Percent", "98.9%", "", "1.0%", + "", "0.0%", "", "100.0%", "", " (1)", " Columns and rows percentages", "", + "", "", "", "" + ) + + test_that("proc_freq executes without errors", { dummy_df <- data.frame( values = rep(letters[1:3], each = 2), @@ -68,3 +82,16 @@ test_that("proc_freq content", { expect_error(proc_freq(dat)) }) + +test_that("proc_freq content no count", { + ft <- proc_freq(dat, + row = "o", col = "p", + include.table_count = FALSE + ) + expect_equal(information_data_chunk(ft)$txt, no_count_full_dump_txt) + + expect_error(proc_freq(dat, + include.table_count = FALSE, + include.table_percent = FALSE) + ) +})