dplyrを関数で使うためのメモ
探索的に使うdplyr.
mtcars %>%
filter(cyl == 6) %>%
rmarkdown::paged_table()
特に問題ない.
my_filter <- function(filter_var, condition, df=mtcars) {
df %>%
filter(filter_var == condition)
}
my_filter(cyl, 6, mtcars) %>% rmarkdown::paged_table()
Problem with `filter()` input `..1`.
x object 'cyl' not found
ℹ Input `..1` is `filter_var == condition`.
どうやら、filter_var
を参照している。cyl
を参照してもらいたい。
enquo()
を使う方法my_filter <- function(filter_var, condition, df=mtcars) {
filter_var <- enquo(filter_var)
df %>%
filter(!!filter_var == condition)
}
my_filter(cyl, 6, mtcars) %>% rmarkdown::paged_table()
{{}}
my_filter2 <- function(filter_var, condition, df=mtcars) {
df %>%
filter({{filter_var}} == condition)
}
my_filter2(cyl, 6, mtcars) %>% rmarkdown::paged_table()
my_select <- function(df=mtcars, ...) {
vars <- enquos(...)
df %>%
select(!!!vars)
}
my_select(mtcars, mpg, cyl) %>% rmarkdown::paged_table()