-
Notifications
You must be signed in to change notification settings - Fork 138
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
add allow_na argument to check character() #1742
Conversation
R/standalone-types-check.R
Outdated
if (is_character(x) & allow_na) { | ||
return(invisible(NULL)) | ||
} | ||
if(is.character(x) & !(TRUE %in% is.na(x)) & !allow_na){ |
There was a problem hiding this comment.
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 (
.)
There was a problem hiding this comment.
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))
.
There was a problem hiding this comment.
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))) |
There was a problem hiding this comment.
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.
R/standalone-types-check.R
Outdated
@@ -459,23 +459,34 @@ check_formula <- function(x, | |||
|
|||
check_character <- function(x, | |||
..., | |||
allow_na = FALSE, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now changed to TRUE
.
852f1a1
to
3e674b8
Compare
Thanks @martaalcalde! |
Addresses part of #1724