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

magic so vet(!is.na(f(.)), x) reports "f(x) should not contain NAs, but does" #86

Closed
franknarf1 opened this issue Nov 23, 2017 · 2 comments

Comments

@franknarf1
Copy link
Contributor

franknarf1 commented Nov 23, 2017

From my recent issue (#85): I'm testing if a numeric input can be interpreted as an 8-digit date:

library(vetr)
library(data.table)

date8_toIDate = function(x) as.IDate(as.character(x), format = "%Y%m%d")
DATE8 = vet_token( INT && !is.na(date8_toIDate(.)) )
x = c(20010102L, 20010100L)

vet(DATE8, x)
# "`!is.na(date8_toIDate(x))` is not all TRUE (contains non-TRUE values)"

It would be nice if the token was somehow magically parsed so that the message was instead...

magicvet(DATE8, x)
# [1] "`date8_toIDate(x)` should not contain NAs, but does" 

Alternately, maybe there could be syntax so I can write my token as..

DATE8 = vet_token( INT && NO.NA(date8_toIDate(.)) )

with the messaging behavior mentioned above.

@franknarf1 franknarf1 changed the title magic so vet(!is.na(f(.)), x) reports "f(x)" should not contain NAs, but does magic so vet(!is.na(f(.)), x) reports "f(x) should not contain NAs, but does" Nov 23, 2017
@brodieG
Copy link
Owner

brodieG commented Nov 23, 2017

A possible solution (and unfortunately of the manual and not magic variety):

DATE8.SUB <- vet_token(
  !anyNA(date8_toIDate(.)),
  "date8_toIdate(%s) should not contain NAs, but does"
)
vet(DATE8.SUB && INT, y)
## [1] "date8_toIdate(`y` ) should not contain NAs, but does"

There is a string formatting issue with an extra space getting injected that I'll have to fix.

Note that you only need to use vet_token if you want to supply a custom message was we do here. In normal use where vet writes its own messages you can just use quote.

@franknarf1
Copy link
Contributor Author

Works for me; thanks. I hadn’t really understood that that’s what that arg is for.

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