Create R function using dplyr::filter problem
From https://dplyr.tidyverse.org/articles/programming.html
Most dplyr functions use non-standard evaluation (NSE). This is a catch-all term that means they don't follow the usual R rules of evaluation.
This can sometimes create a few issues when attempting to wrap them in functions. Here is a base version of the function you created.
mydiff<- function(filteron, df_1=df1, df_2 = df2){
col_1 = paste0(filteron,"x")
col_2 = paste0(filteron, "y")
my_df <- merge(df1, df2, by="id", suffixes = c("x","y"))
my_df[my_df[, col_1] != my_df[, col_2], c("id", col_1, col_2)]
}
> mydiff("a")
id ax ay
3 14 f k
> mydiff("b")
id bx by
5 18 bar foo
This will solve your problem and will likely work as one expects, now and in the future. With less dependencies on outside packages, you reduce these kind of issues and other quirks which may develop in the future as the package authors evolve their work.
The reason it did not work in your original function was that col_1
was string
but dplyr::filter()
expected "unquoted" input variable for the LHS. Thus, you need to first convert col_1
to variable using sym()
then unquote it inside filter
using !!
(bang bang).
rlang
has really nice function qq_show
to show what actually happens with quoting/unquoting (see the output below)
See also this similar question
library(rlang)
library(dplyr)
# creating a function that can take either string or symbol as input
mydiff <- function(filteron, df_1 = df1, df_2 = df2) {
col_1 <- paste0(quo_name(enquo(filteron)), "x")
col_2 <- paste0(quo_name(enquo(filteron)), "y")
my_df <- inner_join(df_1, df_2, by = "id", suffix = c("x", "y"))
cat('\nwithout sym and unquote\n')
qq_show(col_1 != col_2)
cat('\nwith sym and unquote\n')
qq_show(!!sym(col_1) != !!sym(col_2))
cat('\n')
my_df %>%
select(id, col_1, col_2) %>%
filter(!!sym(col_1) != !!sym(col_2))
}
### testing: filteron as a string
mydiff("a")
#>
#> without sym and unquote
#> col_1 != col_2
#>
#> with sym and unquote
#> ax != ay
#>
#> # A tibble: 1 x 3
#> id ax ay
#> <dbl> <chr> <chr>
#> 1 14 f k
### testing: filteron as a symbol
mydiff(a)
#>
#> without sym and unquote
#> col_1 != col_2
#>
#> with sym and unquote
#> ax != ay
#>
#> # A tibble: 1 x 3
#> id ax ay
#> <dbl> <chr> <chr>
#> 1 14 f k
Created on 2018-09-28 by the reprex package (v0.2.1.9000)