Created
July 26, 2017 16:09
-
-
Save brodieG/d2f4384baf1e5bc1894ca50940682588 to your computer and use it in GitHub Desktop.
Illustration on using `vetr` with `date_between`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
date.2 <- rep(Sys.Date(), 2) # length 2 date object | |
date.1 <- date.2[1] | |
valid_identifier_regex <- function() "^\\w+$" | |
date_between <- function(column_name, date_range) { | |
vetr( | |
character(1L) && grepl(valid_identifier_regex(), .), | |
(date.2 || date.1) && !anyNA(.) | |
) | |
TRUE | |
} | |
dates.good <- as.Date(c('2017-07-01', '2017-08-01')) | |
dates.bad.2 <- as.POSIXct(c('2017-07-01', '2017-08-01')) | |
dates.bad.3 <- rep(dates.good, 2) | |
date_between('thiscol', dates.good) | |
## [1] TRUE | |
date_between('this col', dates.good) | |
## Error in date_between(column_name = "this col", date_range = dates.good) : | |
## For argument `column_name`, `grepl(valid_identifier_regex(), "this col")` is not TRUE (FALSE) | |
date_between('thiscol', dates.bad.2) | |
## Error in date_between(column_name = "thiscol", date_range = dates.bad.2) : | |
## For argument `date_range`, `class(dates.bad.2)[2]` should be "Date" (is "POSIXt") | |
date_between('thiscol', dates.bad.3) | |
## Error in date_between(column_name = "thiscol", date_range = dates.bad.3) : | |
## For argument `date_range`, `dates.bad.3` should be length 1, or length 2 (is 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment