From 1c0ee3738b75f57b852443ad81787ee3ed691927 Mon Sep 17 00:00:00 2001 From: yjunechoe Date: Sun, 4 Aug 2024 14:28:02 +0900 Subject: [PATCH 1/2] na_rm argument to snip_list() --- R/info_add.R | 10 ++++++++++ R/utils.R | 5 +++++ man/snip_list.Rd | 7 +++++++ 3 files changed, 22 insertions(+) diff --git a/R/info_add.R b/R/info_add.R index 44739cdb3..00cfbb9eb 100644 --- a/R/info_add.R +++ b/R/info_add.R @@ -1231,6 +1231,12 @@ info_snippet <- function( #' derived from `character` or `factor` values; numbers, dates, and logical #' values won't have quotation marks. We can explicitly use quotations (or #' not) with either `TRUE` or `FALSE` here. +#' +#' @param na_rm *Remove NA values from list* +#' +#' `scalar` // *default:* `FALSE` +#' +#' An option for whether NA values should be counted as an item in the list. #' #' @param lang *Reporting language* #' @@ -1301,6 +1307,7 @@ snip_list <- function( oxford = TRUE, as_code = TRUE, quot_str = NULL, + na_rm = FALSE, lang = NULL ) { @@ -1376,6 +1383,7 @@ snip_list <- function( oxford = <>, as_code = <>, quot_str = <>, + na_rm = <>, lang = <> )", .open = "<<", .close = ">>" @@ -1406,6 +1414,7 @@ snip_list <- function( oxford = <>, as_code = <>, quot_str = <>, + na_rm = <>, lang = <> )", .open = "<<", .close = ">>" @@ -1435,6 +1444,7 @@ snip_list <- function( oxford = <>, as_code = <>, quot_str = <>, + na_rm = <>, lang = <> )", .open = "<<", .close = ">>" diff --git a/R/utils.R b/R/utils.R index ccdf7de1d..aeb52eb53 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1221,11 +1221,16 @@ pb_str_catalog <- function( oxford = TRUE, as_code = TRUE, quot_str = NULL, + na_rm = FALSE, lang = NULL ) { if (is.null(lang)) lang <- "en" + if (na_rm) { + item_vector <- item_vector[!is.na(item_vector)] + } + item_count <- length(item_vector) # If there is nothing in the `item_vector`, return diff --git a/man/snip_list.Rd b/man/snip_list.Rd index 444afb832..d740c2c37 100644 --- a/man/snip_list.Rd +++ b/man/snip_list.Rd @@ -14,6 +14,7 @@ snip_list( oxford = TRUE, as_code = TRUE, quot_str = NULL, + na_rm = FALSE, lang = NULL ) } @@ -90,6 +91,12 @@ derived from \code{character} or \code{factor} values; numbers, dates, and logic values won't have quotation marks. We can explicitly use quotations (or not) with either \code{TRUE} or \code{FALSE} here.} +\item{na_rm}{\emph{Remove NA values from list} + +\verb{scalar} // \emph{default:} \code{FALSE} + +An option for whether NA values should be counted as an item in the list.} + \item{lang}{\emph{Reporting language} \verb{scalar} // \emph{default:} \code{NULL} (\code{optional}) From 05e3c586b0ea9f1de3ae09a6d0ee838b1abdb643 Mon Sep 17 00:00:00 2001 From: yjunechoe Date: Sun, 4 Aug 2024 14:28:08 +0900 Subject: [PATCH 2/2] tests for na_rm --- tests/testthat/test-snip_fns.R | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/testthat/test-snip_fns.R b/tests/testthat/test-snip_fns.R index c50191c56..2a41730e4 100644 --- a/tests/testthat/test-snip_fns.R +++ b/tests/testthat/test-snip_fns.R @@ -511,6 +511,30 @@ test_that("the `snip_list()` function works", { small_table ) ) + + # na_rm ignores NA values in list + expect_match( + run_snip( + snip_list(column = "f", na_rm = FALSE), + small_table %>% dplyr::mutate(f = ifelse(f == "high", NA, f)) + ), + "NA" + ) + expect_no_match( + run_snip( + snip_list(column = "f", na_rm = TRUE), + small_table %>% dplyr::mutate(f = ifelse(f == "high", NA, f)) + ), + "NA" + ) + expect_equal( + run_snip( + snip_list(column = "f", na_rm = TRUE), + small_table %>% dplyr::mutate(f = NA) + ), + "---" + ) + }) test_that("the `snip_stats()` function works", {