tidyeval

How to programmatically filter columns in dplyr?

天涯浪子 提交于 2020-05-12 04:54:31
问题 How would I create a function that drops NA values in a column if I don't want to specify the column until the function is called? minimal_case <- function(column_name = "a") { enquo_name <- enquo(column_name) example <- tibble(a = c(NA, 1)) print(filter(example, !is.na(a))) print(filter(example, !is.na(rlang::UQ(enquo_name)))) } The output of the first print statement is: # A tibble: 1 x 1 a <dbl> 1 1 The output of the second print statement is: # A tibble: 2 x 1 a <dbl> 1 NA 2 1 How do I

dplyr: NSE in joins (by)

牧云@^-^@ 提交于 2020-05-12 04:28:26
问题 I had a hard time to figure out how I could join two tables using dplyr::left_join with NSE. The problem was that I could not supply the right value to 'by'. I think I have found a solution for now, but it feels like I am doing it in an extra complicated way. So, if you know an easier/more elegant solution, please let me know :) That's what I am doing: # some data df <- dplyr::tibble(x=1:10, y=LETTERS[1:10], z=LETTERS[11:20]) # some function test_fun <- function(df,id){ id <- rlang::enquo(id)

Get expression that evaluated to dot in function called by `magrittr`

删除回忆录丶 提交于 2020-03-18 12:43:25
问题 I have a function x_expression() which prints the expression passed to argument x . pacman::p_load(magrittr, rlang) x_expression <- function(x) { print(enquo(x)) } y <- 1 x_expression(y) #> <quosure> #> expr: ^y #> env: global y %>% x_expression() #> <quosure> #> expr: ^. #> env: 0x7ff27c36a610 So you can see that it knows y was passed to it, but when y is piped in with %>% , the function returns prints . . Is there a way to recover the y in the case that it is piped in, or is it gone forever

Get expression that evaluated to dot in function called by `magrittr`

回眸只為那壹抹淺笑 提交于 2020-03-18 12:43:19
问题 I have a function x_expression() which prints the expression passed to argument x . pacman::p_load(magrittr, rlang) x_expression <- function(x) { print(enquo(x)) } y <- 1 x_expression(y) #> <quosure> #> expr: ^y #> env: global y %>% x_expression() #> <quosure> #> expr: ^. #> env: 0x7ff27c36a610 So you can see that it knows y was passed to it, but when y is piped in with %>% , the function returns prints . . Is there a way to recover the y in the case that it is piped in, or is it gone forever

Tidyeval splice operator !!! fails with ggplot's aes

你离开我真会死。 提交于 2020-03-03 04:38:26
问题 The article discussing tidy evaluation in ggplot2 gives the impression that aes() now supports quasiquoation. However, I'm having problems getting it to work with the unquote-splice operator !!! . library( ggplot2 ) ## Predefine the mapping of symbols to aesthetics v <- rlang::exprs( x=wt, y=mpg ) ## Symbol-by-symbol unquoting works without problems ggplot( mtcars, aes(!!v$x, !!v$y) ) + geom_point() ## But unquote splicing doesn't... ggplot( mtcars, aes(!!!v) ) + geom_point() # Error: Can't

What is the difference between ensym and enquo when programming with dplyr?

筅森魡賤 提交于 2020-02-22 07:37:06
问题 Relatively new to tidy evaluation and while the functions I'm making work, I want to know why different helper functions are used. For example, what is the difference between enquo and ensym ? In the function I made below to capture daily average and moving average they're interchangeable: library(dplyr) library(lubridate) library(rlang) library(zoo) manipulate_for_ma <- function(data, group_var, da_col_name, summary_var, ma_col_name) { group_var <- ensym(group_var) summary_var <- enquo

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

妖精的绣舞 提交于 2020-01-13 18:11:15
问题 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

Supplying multiple groups of variables to a function for dplyr arguments in the body

六眼飞鱼酱① 提交于 2020-01-10 05:05:08
问题 Here is the data: library(tidyverse) data <- tibble::tribble( ~var1, ~var2, ~var3, ~var4, ~var5, "a", "d", "g", "hello", 1L, "a", "d", "h", "hello", 2L, "b", "e", "h", "k", 4L, "b", "e", "h", "k", 7L, "c", "f", "i", "hello", 3L, "c", "f", "i", "hello", 4L ) and the vectors, I want to use: filter_var <- c("hello") groupby_vars1 <- c("var1", "var2", "var3") groupby_vars2 <- c("var1", "var2") joinby_vars1 <- c("var1", "var2") joinby_vars2 <- c("var1", "var2", "var3") 2nd & 5th, and 3rd & 4th

Dplyr Multiple Lags Tidy Eval?

Deadly 提交于 2020-01-04 05:55:24
问题 I am trying to make multiple lags using the least amount of code possible in dplyr, while sticking to tidy eval. The following Standard Evaluation (SE) code works: #if(!require(dplyr)) install.packages("dplyr"); library(dplyr) a=as_tibble(c(1:100)) lags=3 lag_prefix=paste0("L", 1:lags, ".y") multi_lag=setNames(paste("lag(.,", 1:lags, ")"), lag_prefix) a %>% mutate_at(vars(value), funs_(multi_lag)) #final line # A tibble: 100 x 4 value L1.y L2.y L3.y <int> <int> <int> <int> 1 1 NA NA NA 2 2 1

Excluding multiple columns based on unquote-splicing (!!!)

ぃ、小莉子 提交于 2020-01-02 05:14:08
问题 Trying to exclude multiple columns in a call to tidyr::gather() which are served as inputs to my function via a character vector argument (output of shiny::selectInput ) instead of via ... in a programmatic way How would I do that with tidy eval functionality? Since I pass multiple column names via a single function argument, I thought I needed to use !!! (unquote-splicing) instead of !! as layed out in Programming with dplyr. But that doesn't seem to play nicely with tidyselect::vars_select(