Skip to content

Commit

Permalink
Merge pull request #556 from yjunechoe/remove-na-snip_list
Browse files Browse the repository at this point in the history
Add `snip_list(na_rm)` argument
  • Loading branch information
rich-iannone authored Aug 6, 2024
2 parents f9fc1f3 + 05e3c58 commit 856a7f4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions R/info_add.R
Original file line number Diff line number Diff line change
Expand Up @@ -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<logical>` // *default:* `FALSE`
#'
#' An option for whether NA values should be counted as an item in the list.
#'
#' @param lang *Reporting language*
#'
Expand Down Expand Up @@ -1301,6 +1307,7 @@ snip_list <- function(
oxford = TRUE,
as_code = TRUE,
quot_str = NULL,
na_rm = FALSE,
lang = NULL
) {

Expand Down Expand Up @@ -1376,6 +1383,7 @@ snip_list <- function(
oxford = <<oxford>>,
as_code = <<as_code>>,
quot_str = <<quot_str>>,
na_rm = <<na_rm>>,
lang = <<lang>>
)",
.open = "<<", .close = ">>"
Expand Down Expand Up @@ -1406,6 +1414,7 @@ snip_list <- function(
oxford = <<oxford>>,
as_code = <<as_code>>,
quot_str = <<quot_str>>,
na_rm = <<na_rm>>,
lang = <<lang>>
)",
.open = "<<", .close = ">>"
Expand Down Expand Up @@ -1435,6 +1444,7 @@ snip_list <- function(
oxford = <<oxford>>,
as_code = <<as_code>>,
quot_str = <<quot_str>>,
na_rm = <<na_rm>>,
lang = <<lang>>
)",
.open = "<<", .close = ">>"
Expand Down
5 changes: 5 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions man/snip_list.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/testthat/test-snip_fns.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit 856a7f4

Please sign in to comment.