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 allow_na argument to check character() #1742

Merged

Conversation

martaalcalde
Copy link
Contributor

@martaalcalde martaalcalde commented Aug 15, 2024

Addresses part of #1724

@martaalcalde
Copy link
Contributor Author

martaalcalde commented Aug 15, 2024

Although the error message that it appears now may not be very intuitive:

Screenshot 2024-08-15 at 12 04 28

if (is_character(x) & allow_na) {
return(invisible(NULL))
}
if(is.character(x) & !(TRUE %in% is.na(x)) & !allow_na){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These checks should use the scalar boolean operators, e.g. &&. (Also missing a space between if and (.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simpler way of expressing TRUE %in% is.na(x) is any(is.na(x)).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To solve the error message I think I'd structure this code like this:

if (is_character(x)) {
  if (!allow_na && any(is.na(x))) {
    abort(...)
  }
  return(invisible(NULL))
}

With a specially crafted error message in the abort() call.

expect_null(check_character(NULL, allow_null = TRUE))
expect_error(check_character(NULL, allow_null = FALSE))

expect_error(check_character(c("a",NA)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be tested in the expect_snapshot() block below so the error message is tracked.

@lionel- lionel- reopened this Aug 15, 2024
@@ -459,23 +459,34 @@ check_formula <- function(x,

check_character <- function(x,
...,
allow_na = FALSE,
Copy link
Contributor

@olivroy olivroy Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a breaking change here? The default should be TRUE imo. Because, at the moment, it always allows NA as suggests the issue title

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now changed to TRUE.

@lionel- lionel- force-pushed the add-allowna-argument-to-check-character branch from 852f1a1 to 3e674b8 Compare August 16, 2024 01:07
@lionel- lionel- merged commit 69659c4 into r-lib:main Aug 16, 2024
11 checks passed
@lionel-
Copy link
Member

lionel- commented Aug 16, 2024

Thanks @martaalcalde!

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

Successfully merging this pull request may close these issues.

3 participants