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 there a way to assert elementwise over a whole data.frame? #77

Closed
sursh opened this issue Sep 25, 2018 · 6 comments
Closed

Is there a way to assert elementwise over a whole data.frame? #77

sursh opened this issue Sep 25, 2018 · 6 comments

Comments

@sursh
Copy link

sursh commented Sep 25, 2018

Hi there, this library looks awesome! My first use case for it is to check an entire data.frame, let's say for NaNs.

I wrote a function contains_no_nans that returns FALSE if there are any NaNs in my data.frame. It's working as expected when I do df %>% contains_no_nans. However, I'm not sure how to use it with assertr. To my surprise, this passed even when there were NaNs:

my_df %>% assert(contains_no_nans)

I was surprised because I was extrapolating from your example assert(within_bounds(0,Inf, include.lower=FALSE), -mpg). I figured that looks at everything except mpg, so if I leave out the second argument it should look at all columns.

I suspect it's because you're using select under the hood and if you don't pass any arguments into select(), it returns an Nx0 tibble which, well, has no NaNs. So a workaround would be to call it like this:

my_df %>% assert(contains_no_nans, colnames(.))

Is this the best way?

And whether it is or isn't, perhaps consider adding a note about this to the vignette. It's true that you don't claim that assert can do this, but it's a subtle thing and if people are in a hurry it could lead to asserts passing when they shouldn't.

Thanks again for this great library! This will save me lots of heartache.

@richardjtelford
Copy link

You probably need
my_df %>% assert(contains_no_nans, everything())
so assert knows to use all columns.

@tonyfischetti
Copy link
Owner

@sursh Thank you so much for the kind words about the library!
@richardjtelford is right, I decided to force the columns to be put in explicitly to avoid errors. Do you have a strong preference otherwise?

@richardjtelford
Copy link

richardjtelford commented Jun 10, 2019

I agree with this behaviour but it might be a good idea to throw an error if ... is empty. So

assert(iris, not_na)

Will fail with an error like "Assert requires columns to be selected"

but

assert(iris, not_na, matches("dog"))

should succeed.

Should only need a couple of lines of code - I can submit a push request later this week to demonstrate exactly what I mean.

@sursh
Copy link
Author

sursh commented Jun 10, 2019

Yes, I'd definitely suggest erring on the side of throwing an error—much safer that way.

@richardjtelford that sounds great! You could make the error message even more helpful like this, telling them how to run it over all columns:

"Assert requires columns to be selected. Select all columns with everything()"

@tonyfischetti
Copy link
Owner

That sounds like a great idea!

tonyfischetti added a commit that referenced this issue Jun 11, 2019
@tonyfischetti
Copy link
Owner

Done! Thanks for reporting it :)
I'm going to try to address some other things and then I'll upload to CRAN. In the meantime, feel free to install it from github with devtools::install_github("ropensci/assertr")

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

3 participants