rlang

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

Use dplyr coalesce in programming

谁说我不能喝 提交于 2019-12-05 08:23:16
I'd like to use dplyr's programming magic , new to version 0.7.0, to coalesce two columns together. Below, I've listed out a few of my attempts. df <- data_frame(x = c(1, 2, NA), y = c(2, NA, 3)) # What I want to do: mutate(df, y = coalesce(x, y)) # Here's the expected output: #> # A tibble: 3 x 2 #> x y #> <dbl> <dbl> #> 1 1 1 #> 2 2 2 #> 3 NA 3 I thought fn1 would work, but it's treating varname as character on the right-hand side. fn1 <- function(varname) { mutate(df, UQ(varname) := coalesce(x, !!varname)) } fn1("y") # Error in mutate_impl(.data, dots) : # Evaluation error: Argument 2 must

Using quotations inside mutate: an alternative to mutate_(.dots = …)

六月ゝ 毕业季﹏ 提交于 2019-12-05 06:52:21
I want to apply different functions to the same column in a tibble. These functions are stored in a character string. I used to do this with mutate_ and the .dots argument like this: library(dplyr) myfuns <- c(f1 = "a^2", f2 = "exp(a)", f3 = "sqrt(a)") tibble(a = 1:3) %>% mutate_(.dots = myfuns) This approach still works fine but mutate_ is deprecated. I tried to achieve the same result with mutate and the rlang package but did not get very far. In my real example myfuns contains about 200 functions so typing them one by one is not an option. Thanks in advance. Convert your strings to

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 <-

R quo_name equivalent of quos

試著忘記壹切 提交于 2019-12-04 19:23:42
问题 Hi following Programming with dplyr I noticed that one can add a name using quo_name. I was wondering how to do this for multiple columns, eg. like a quos_name of sorts. E.g.: my_mutate <- function(df, expr) { expr <- enquo(expr) mean_name <- paste0("mean_", quo_name(expr)) sum_name <- paste0("sum_", quo_name(expr)) mutate(df, !!mean_name := mean(!!expr), !!sum_name := sum(!!expr) ) } becomes my_mutate <- function(df, ...) { exprs <-quos(...) mean_names <- paste0("mean_", quos_name(exprs))

passing arguments to function expr() in rlang and the !! operator

时光总嘲笑我的痴心妄想 提交于 2019-12-04 18:32:56
Define an expression > xy <- expr(x+y) Use it to build a second expression ... and it works > expr(a + !!xy) a + (x + y) simply changing the order of the arguments and it stops working > expr(!!xy + a) Error in (function (x) : object 'a' not found Am I missing something? Thanks There is way to make it work. Change the way !!xy has been used in expr and it will work. i.e expr((!!xy) + a) #(x + y) + a The reason is that priority of all arithmetic and comparison operators are higher than ! . Hence arithmetic and comparison operators binds tightly than ! . e.g.: > expr(!!2 + 3) [1] 5 > expr((!!2)

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:

Using dplyr filter() in programming

北慕城南 提交于 2019-12-04 07:08:38
I am writing my function and want to use dplyr's filter() function to select rows of my data frame that satisfy a condition. This is my code: library(tidyverse) df <-data.frame(x = sample(1:100, 50), y = rnorm(50), z = sample(1:100,50), w = sample(1:100, 50), p = sample(1:100,50)) new <- function(ang,brad,drau){ df%>%filter(!!drau %in% 1:50)%>%select(ang,brad) -> A return(A) } brand <- c("z","w","p") lapply(1:3, function(i) new(ang = "x", brad = "y", drau = brand[i]))%>%bind_rows() Anytime I run this function, it looks like filter doesn't select any rows that satisfy the condition. How can I

Defunct as of rlang 0.3.0 and mutate_impl

房东的猫 提交于 2019-12-04 00:21:40
I am trying to use the following function but every time I do, I receive the error below. I tried installing an older version of rlang as it works on a different R Studio but I was unable to do that. It seems the error is due to the 0.3.0 version. Any suggestions on how to fix this error would be appreciated. details2 <- details %>% mutate(rownames=rownames(.)) %>% filter(isdir==FALSE) %>% arrange(desc(ctime)) Error in mutate_impl(.data, dots) : Evaluation error: `as_dictionary()` is defunct as of rlang 0.3.0. Please use `as_data_pronoun()` instead. To solve this issue within a docker

How to feed a list of unquoted column names into `lapply` (so that I can use it with a `dplyr` function)

血红的双手。 提交于 2019-12-03 17:15:34
问题 I am trying to write a function in tidyverse/dplyr that I want to eventually use with lapply (or map ). (I had been working on it to answer this question, but came upon an interesting result/dead-end. Please don't mark this as a duplicate - this question is an extension/departure from the answers that you see there.) Is there 1) a way to get a list of quoted variables to work inside a dplyr function (and not use the deprecated SE_ functions) or is there 2) some way to feed a list of unquoted