rlang

converting a quosure to a string in R

早过忘川 提交于 2020-01-23 05:48:27
问题 I've been using quosures with dplyr: library(dplyr) library(ggplot2) thing <- quo(clarity) diamonds %>% select(!!thing) print(paste("looking at", thing)) [1] "looking at ~" "looking at clarity" I really want to print out the string value put into the quo, but can only get the following: print(thing) <quosure: global> ~clarity print(thing[2]) clarity() substr(thing[2],1, nchar(thing[2])) [1] "clarity" is there a simpler way to "unquote" a quo()? 回答1: We can use quo_name print(paste("looking at

dplyr: mutate_at + coalesce: dynamic names of columns

◇◆丶佛笑我妖孽 提交于 2020-01-22 14:06:32
问题 I've been trying for awhile to combine mutate_at with coalesce in case in which names of columns are generated dynamically. In my example there are only five columns, but in the real data there are much more (and not all columns should be included in coalesce step). Example DF: data_example <- data.frame( aa = c(1, NA, NA), bb = c(NA, NA, 2), cc = c(6, 7, 8), aa_extra = c(2, 2, NA), bb_extra = c(1, 2, 3) ) Expected output: aa bb cc aa_extra bb_extra 1 1 1 6 2 1 2 2 2 7 2 2 3 NA 2 8 NA 3

dplyr: mutate_at + coalesce: dynamic names of columns

时光毁灭记忆、已成空白 提交于 2020-01-22 14:06:08
问题 I've been trying for awhile to combine mutate_at with coalesce in case in which names of columns are generated dynamically. In my example there are only five columns, but in the real data there are much more (and not all columns should be included in coalesce step). Example DF: data_example <- data.frame( aa = c(1, NA, NA), bb = c(NA, NA, 2), cc = c(6, 7, 8), aa_extra = c(2, 2, NA), bb_extra = c(1, 2, 3) ) Expected output: aa bb cc aa_extra bb_extra 1 1 1 6 2 1 2 2 2 7 2 2 3 NA 2 8 NA 3

group_by by a vector of characters using tidy evaluation semantics

自闭症网瘾萝莉.ら 提交于 2020-01-15 11:07:46
问题 I used to do it, using group_by_ library(dplyr) group_by <- c('cyl', 'vs') mtcars %>% group_by_(.dots = group_by) %>% summarise(gear = mean(gear)) but now group_by_ is deprecated. I don't know how to do it using the tidy evaluation framework. 回答1: There is group_by_at variant of group_by : library(dplyr) group_by <- c('cyl', 'vs') mtcars %>% group_by_at(group_by) %>% summarise(gear = mean(gear)) Above it's simplified version of generalized: mtcars %>% group_by_at(vars(one_of(group_by))) %>%

What is the difference between sym() and parse_expr() in the rlang package?

佐手、 提交于 2020-01-14 10:25:14
问题 Using the rlang package, I wonder what is the difference between sym() and parse_expr() . Consider for example the following expressions: ex1 = sym('a') ex2 = parse_expr('a') They both return a identical(ex1, ex2) [1] TRUE Suppose now I need a quosure: ex3 = quo(!!sym('a')) ex4 = quo(!!parse_expr('a')) In both case, the result is: <quosure> expr: ^a env: global identical(ex3, ex4) [1] TRUE However, the two following are not the same for some reasons. ex5 = quo(!!sym('a - b')) ex6 = quo(!

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

Custom pipe to silence warnings

ε祈祈猫儿з 提交于 2020-01-10 17:46:09
问题 Related to this question. I'd like to build a custom pipe %W>% that would silence warnings for one operation library(magrittr) data.frame(a= c(1,-1)) %W>% mutate(a=sqrt(a)) %>% cos will be equivalent to : w <- options()$warn data.frame(a= c(1,-1)) %T>% {options(warn=-1)} %>% mutate(a=sqrt(a)) %T>% {options(warn=w)} %>% cos These two tries don't work : `%W>%` <- function(lhs,rhs){ w <- options()$warn on.exit(options(warn=w)) options(warn=-1) lhs %>% rhs } `%W>%` <- function(lhs,rhs){ lhs <-

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

properly evaluating `rlang` expression inside list column

走远了吗. 提交于 2020-01-06 13:10:46
问题 This is a follow-up question to (using `rlang` for conditional labelling in `ggplot` using `ggrepel`), which solved the problem I was facing in a custom function using an expression to filter out data while labeling data points. But the answer raised another issue that I don't know how to solve. Here is the custom function that uses rlang to evaluate the user-entered expression to filter out data while attaching labels to data points. And this function works fine when not used inside list

properly evaluating `rlang` expression inside list column

守給你的承諾、 提交于 2020-01-06 13:10:00
问题 This is a follow-up question to (using `rlang` for conditional labelling in `ggplot` using `ggrepel`), which solved the problem I was facing in a custom function using an expression to filter out data while labeling data points. But the answer raised another issue that I don't know how to solve. Here is the custom function that uses rlang to evaluate the user-entered expression to filter out data while attaching labels to data points. And this function works fine when not used inside list