Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snip_list(na_rm) argument #556

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading