tidyeval

Correct usage of dplyr::select in dplyr 0.7.0+, selecting columns using character vector

会有一股神秘感。 提交于 2019-12-09 10:01:47
问题 Suppose we have a character vector cols_to_select containing some columns we want to select from a dataframe df , e.g. df <- tibble::data_frame(a=1:3, b=1:3, c=1:3, d=1:3, e=1:3) cols_to_select <- c("b", "d") Suppose also we want to use dplyr::select because it's part of an operation that uses %>% so using select makes the code easy to read. There seem to be a number of ways which this can be achieved, but some are more robust than others. Please could you let me know which is the 'correct'

using non-standard evaluation with formula

丶灬走出姿态 提交于 2019-12-08 13:24:39
问题 I'm creating a package that uses non-standard evaluation to keep track of the meaning of columns. The package passes a data frame among functions, which do various things do the same set of columns. Nonstandard evaluation works great for this: my_select <- function(df, xcol, ycol) { new_df <- dplyr::select(df, !!xcol, !!ycol) new_df } my_select(mtcars, quo(wt), quo(mpg)) However, I'd like a function that works with a formula: my_lm <- function(df, xcol, ycol) { new_lm <- lm(!!xcol, !!ycol,

Tidyeval: pass list of columns as quosure to select()

烈酒焚心 提交于 2019-12-07 14:32:05
问题 I want to pass a bunch of columns to pmap() inside mutate() . Later, I want to select those same columns. At the moment, I'm passing a list of column names to pmap() as a quosure, which works fine, although I have no idea whether this is the "right" way to do it. But I can't figure out how to use the same quosure/list for select() . I've got almost no experience with tidyeval, I've only got this far by playing around. I imagine there must be a way to use the same thing both for pmap() and

What is the tidyeval way of using dplyr::filter?

亡梦爱人 提交于 2019-12-07 04:46:55
问题 Call the function below using foo(c("b")) . The outputs are shown inline. What is the right way of writing df %>% filter(!!x > (!!x)) ? I have included an example of using mutate in tidyeval style to contrast it with filter . foo <- function(variables) { x <- rlang::sym(variables[[1]]) print(x) #> b print(typeof(x)) #> [1] "symbol" df <- data_frame(a = 1, b = 2) print(df %>% mutate(!!x := 100 + !!x)) #> # A tibble: 1 x 2 #> a b #> <dbl> <dbl> #> 1 1 102 print(df %>% filter(!!x > (!!x))) #>

Using pre-existing character vectors in quasiquotation of an expression with rlang

主宰稳场 提交于 2019-12-05 20:56:44
Sometimes when working with dplyr one has a character vector of column names that's to be used to operate on data, for instance: cols_of_interest <- c("Petal.Width", "Petal.Length") In dplyr 0.5.0 and earlier, the recommended approach to this problem was to use the verb_ underscore construct as follows: library("tidyverse") my_cols <- c("Petal.Width", "Petal.Length") iris %>% select_(.dots = my_cols) The verb_ functions are now deprecated in favour of the new tidy evaluation framework (dplyr.tidyverse.org/articles/programming.html) introduced by the rlang library. As of dplyr 0.7.0 the

What is the tidyeval way of using dplyr::filter?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 09:09:54
Call the function below using foo(c("b")) . The outputs are shown inline. What is the right way of writing df %>% filter(!!x > (!!x)) ? I have included an example of using mutate in tidyeval style to contrast it with filter . foo <- function(variables) { x <- rlang::sym(variables[[1]]) print(x) #> b print(typeof(x)) #> [1] "symbol" df <- data_frame(a = 1, b = 2) print(df %>% mutate(!!x := 100 + !!x)) #> # A tibble: 1 x 2 #> a b #> <dbl> <dbl> #> 1 1 102 print(df %>% filter(!!x > (!!x))) #> Error in !x : invalid argument type print(df %>% filter(magrittr::is_greater_than(!!x, !!x))) #> # A

rlang::sym in anonymous functions

烈酒焚心 提交于 2019-12-05 01:01:16
I recently notices that rlang::sym doesn't seem to work in anonymous functions and I don't understand why. Here an example, it's pretty clumsy and ugly but I think it illustrates the point require(tidyverse) data <- tibble(x1 = letters[1:3], x2 = letters[4:6], val = 1:3) get_it <- function(a, b){ data %>% mutate(y1 = !!rlang::sym(a)) %>% mutate(y2 = !!rlang::sym(b)) %>% select(y1, y2, val) } get_it("x1", "x2") This defines some toy data and a (horrible) function that essentially renames the columns based on column names. Now I can do the same thing for different combinations of a and b: d <-

Tidy evaluation programming and ggplot2

谁都会走 提交于 2019-12-04 17:17:32
问题 Trying to write a relatively simple wrapper to produce some plots, but can not work out how to specify tidy evaluation of grouping variables specified as ... an example function that facets variables but doesn't distinguish by grouping... my_plot <- function(df = starwars, select = c(height, mass), ...){ results <- list() ## Tidyeval arguments quo_select <- enquo(select) quo_group <- quos(...) ## Filter, reshape and plot results$df <- df %>% dplyr::filter(!is.na(!!!quo_group)) %>% dplyr:

Correct usage of dplyr::select in dplyr 0.7.0+, selecting columns using character vector

耗尽温柔 提交于 2019-12-03 14:01:57
Suppose we have a character vector cols_to_select containing some columns we want to select from a dataframe df , e.g. df <- tibble::data_frame(a=1:3, b=1:3, c=1:3, d=1:3, e=1:3) cols_to_select <- c("b", "d") Suppose also we want to use dplyr::select because it's part of an operation that uses %>% so using select makes the code easy to read. There seem to be a number of ways which this can be achieved, but some are more robust than others. Please could you let me know which is the 'correct' version and why? Or perhaps there is another, better way? dplyr::select(df, cols_to_select) #Fails if

Tidy evaluation programming and ggplot2

心不动则不痛 提交于 2019-12-03 12:21:08
Trying to write a relatively simple wrapper to produce some plots, but can not work out how to specify tidy evaluation of grouping variables specified as ... an example function that facets variables but doesn't distinguish by grouping... my_plot <- function(df = starwars, select = c(height, mass), ...){ results <- list() ## Tidyeval arguments quo_select <- enquo(select) quo_group <- quos(...) ## Filter, reshape and plot results$df <- df %>% dplyr::filter(!is.na(!!!quo_group)) %>% dplyr::select(!!quo_select, !!!quo_group) %>% gather(key = variable, value = value, !!!quo_select) %>% ## Specify