I have simple code that creates arbitrary example data:
library(assertr)
library(tidyverse)
set.seed(1)
df <- tibble(id = 1:10, value = rnorm(10, 0, 1)) %>%
mutate(value = if_else(abs(value) < 0.5, NA_real_, value))
The data looks like this:
> df
# A tibble: 10 x 2
id value
<int> <dbl>
1 1 -0.626
2 2 NA
3 3 -0.836
4 4 1.60
5 5 NA
6 6 -0.820
7 7 NA
8 8 0.738
9 9 0.576
10 10 NA
Now, I'm trying to write a function that checks if any rows in a given column (in this case, the value
column) have NA values and throws an error if they do. If they don't, it should return the original data, unmodified, so that the pipe can continue. This is simple without a function:
df %>% verify(sum(is.na(value)) == 0)
# Outputs "Error: assertr stopped execution"
Wrapping this in a function causes difficulty, however. I tried using lazyeval
:
verify_not_missing <- function(.data, v) {
.data %>% verify(sum(is.na(lazyeval::lazy(v))) == 0)
}
df %>% verify_not_missing(value)
But this doesn't throw any error or stop execution. It silently continues execution. Similarly, from the dplyr programming vignette, I thought the following would work:
verify_not_missing <- function(.data, v) {
.data %>% verify(sum(is.na(!! quo(v))) == 0)
}
df %>% verify_not_missing(value)
but that throws an error:
Error in is_quosure(e2) : argument "e2" is missing, with no default
I searched through some of the documentation and SO, including this question, but some of the answers mention deprecated parts of dplyr
that aren't much help (case in point, calling vignette("nse")
reveals that the vignette no longer exists).
What am I missing here?
I'm using R v3.5.1, dplyr v0.7.7, and assertr v2.5 on an x64 Linux system