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

is_bool() #695

Closed
lionel- opened this issue Dec 12, 2018 · 7 comments
Closed

is_bool() #695

lionel- opened this issue Dec 12, 2018 · 7 comments
Labels
Milestone

Comments

@lionel-
Copy link
Member

@lionel- lionel- commented Dec 12, 2018

Like is_string(), for checking inputs:

is_bool <- function(x) {
  is_logical(x, n = 1) && !is.na(x)
}
@lionel- lionel- added the vector label Dec 12, 2018
@lionel-
Copy link
Member Author

@lionel- lionel- commented Dec 12, 2018

And is_number()

is_number <- function(x) {
  is_integerish(x, n = 1, finite = TRUE)
}

@s-fleck
Copy link

@s-fleck s-fleck commented Jan 11, 2019

Just my 2c: I would go is_bool() for an arbitrary length 2-value-logic vector and is_scalar_bool() for a vector of length 1 for consistency. I also often define an is_n() function which checks for positive, non-NA integerishs (as in natural numbers). This is something one often actually wants when doing an argument check for scalar integerishs.

@lionel-
Copy link
Member Author

@lionel- lionel- commented Jan 11, 2019

I prefer is_bool() for single TRUE/FALSE because I think scalar input checks are more important. But we could add an argument to is_logical(), similar to the finite = parameter of numeric predicates.

I like is_n(), maybe it could be is_length()?

@s-fleck
Copy link

@s-fleck s-fleck commented Jan 11, 2019

hmm.. from the name I would assume is_length() to be something like as function(x, y) length(x) == y.

I commented on this issue because I am already using a function is_bool()/is_scalar_bool() across my packages, though i have to confess I almost exclusively need the scalar version (as you have pointed out).

Still it would be more consistent with other rlang predicates. When is_integer() checks for an integer vector, why does is_bool() not check for a boolean vector? There is also is.flag() in assertthat (which is basically is_scalar_logical()), but that is also not really optimal in my oppionion :/

@lionel-
Copy link
Member Author

@lionel- lionel- commented Jan 11, 2019

from the name I would assume is_length() to be something like as function(x, y) length(x) == y.

Good point.

why does is_bool() not check for a boolean vector?

Because those are called logical vectors. I don't think is_bool() would be a consistent name for checking logical vectors. But it is consistent with is_string() / is_character().

@s-fleck
Copy link

@s-fleck s-fleck commented Jan 11, 2019

oh sorry I meant is_bool() for true (2 valued/non-na) boolean vectors as opposed to is_logical() which may contain NAs. though I'm not sure how often you really need those in practice (maybe for indexing/subsetting ) ?

@DavisVaughan
Copy link
Member

@DavisVaughan DavisVaughan commented Jan 24, 2019

+1 for is_bool(), as this would definitely come in handy when type checking some argument inputs!

@lionel- lionel- added this to the 0.4.0 milestone Jun 11, 2019
lionel- added a commit to lionel-/rlang that referenced this issue Jun 11, 2019
lionel- added a commit to lionel-/rlang that referenced this issue Jun 12, 2019
@lionel- lionel- closed this in #786 Jun 12, 2019
lionel- added a commit that referenced this issue Jun 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants