brodieG / vetr

Trust, but Verify
79 stars 2 forks source link

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

Closed franknarf1 closed 6 years ago

franknarf1 commented 6 years ago

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.

brodieG commented 6 years ago

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 commented 6 years ago

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