Created
April 3, 2017 12:01
-
-
Save jonocarroll/4853b87cb50d45ca9ec288ca05a1e0aa to your computer and use it in GitHub Desktop.
Regex on assertr output
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
| library(dplyr) | |
| library(assertr) | |
| # if we have the command | |
| data.frame(x = 1:2) %>% assert(in_set(1), x) | |
| #> Column 'x' violates assertion 'in_set(1)' 1 time | |
| #> index value | |
| #> 1 2 2 | |
| #> Error: assertr stopped execution | |
| # this produces the output | |
| out_txt <- "Column 'x' violates assertion 'in_set(1)' 1 time" | |
| # the original suggestion to extract the column name was | |
| gsub("^Column \'([a-zA-Z]+)\' .*$", '\\1', out_txt) | |
| #> [1] "x" | |
| # which works fine. | |
| # if, however, the columns have real-world names, we have | |
| data.frame(c3po.x = 1:2) %>% assert(in_set(1), c3po.x) | |
| #> Column 'c3po.x' violates assertion 'in_set(1)' 1 time | |
| #> index value | |
| #> 1 2 2 | |
| #> Error: assertr stopped execution | |
| # which produces the output | |
| out_txt <- "Column 'c3po.x' violates assertion 'in_set(1)' 1 time" | |
| # then the regex chokes and returns the whole string | |
| gsub('^Column \'([a-zA-Z]+)\' .*$', '\\1', out_txt) | |
| #> [1] "Column 'c3po.x' violates assertion 'in_set(1)' 1 time" | |
| # a better regex might be (\\S == non-whitespace) | |
| gsub('Column \'(\\S*?)\'.*$', '\\1', out_txt) | |
| #> [1] "c3po.x" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment