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

NAs in pluralize vector cause values to not display correctly #717

Open
rundel opened this issue Aug 15, 2024 · 3 comments
Open

NAs in pluralize vector cause values to not display correctly #717

rundel opened this issue Aug 15, 2024 · 3 comments

Comments

@rundel
Copy link
Contributor

rundel commented Aug 15, 2024

See the following reprex:

library(cli)

pluralize('{c(NA_character_)} file{?s}')
#> NA file
pluralize('{c("file1", NA_character_)} file{?s}')
#> NA files
pluralize('{c(NA_character_, "file1")} file{?s}')
#> NA files
pluralize('{c("file1", "file2", NA_character_)} file{?s}')
#> NA files

Created on 2024-08-15 with reprex v2.1.1

I believe the expected behavior should be to have the vector displayed as is with the included NAs.

@rundel
Copy link
Contributor Author

rundel commented Aug 15, 2024

Seems like this is expected behavior, the underlying cause appears to be in

cli/R/glue.R

Lines 152 to 163 in 2ddcc1a

collapse_head <- function(x, sep, sep2, last, trunc, width, ellipsis) {
trunc <- max(trunc, 1L)
x <- as.character(x)
lnx <- length(x)
# special cases that do not need trimming
if (lnx == 0L) {
return("")
} else if (any(is.na(x))) {
return(NA_character_)
}

Seems undesirable to me, but maybe this behavior is necessary elsewhere outside of pluralize().

@gaborcsardi
Copy link
Member

If the "pluralizing quantity" is a character vector, then the length of the vector is used. E.g. to be able to write:

fs <- c("foo", "bar")
cli_text('Reading file{?s}: {fs}')
#> Reading files: foo and bar

It does not matter if the character vector contains NA elements or not:

v <- c(NA_character_, "bar")
cli_text("Got element{?s}: {v}.")
#> Got elements: NA and bar.

@rundel
Copy link
Contributor Author

rundel commented Aug 18, 2024

The difference seems to be between pluralize() and cli_text() the latter is doing the correct processing while the former is collapsing to just NA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants