I use dplyrand check the order. I am confused about how to check that someone put an object without an object against a string for NSE. For example, I would like to filter out missing data:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df = data_frame(
myvar = c(rep("yes", 2), NA)
)
myfun <- function(x){
x = enquo(x)
num = df %>%
filter(!is.na( !! x))
return(num)
}
myfun(myvar)
#> # A tibble: 2 x 1
#> myvar
#> <chr>
#> 1 yes
#> 2 yes
I would like a string equivalent to error to be, if possible. This currently gives the “wrong” result, as it is is.na("myvar")never FALSE.
myfun("myvar") # wrong result
#> # A tibble: 3 x 1
#> myvar
#> <chr>
#> 1 yes
#> 2 yes
#> 3 <NA>
After viewing What is the tidyeval way of using dplyr :: filter? seems to filter_atallow both scripts to work fine:
myfun <- function(x){
x = enquo(x)
num = df %>%
filter_at(vars( !! x), all_vars(!is.na(.)))
return(num)
}
myfun(myvar)
#> # A tibble: 2 x 1
#> myvar
#> <chr>
#> 1 yes
#> 2 yes
myfun("myvar") # correct result
#> # A tibble: 2 x 1
#> myvar
#> <chr>
#> 1 yes
#> 2 yes
myfun("myvar") ? colnames(), , , , as.name.