rlang

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

故事扮演 提交于 2020-01-01 19:47:21
问题 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 回答1: 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 !

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

 ̄綄美尐妖づ 提交于 2020-01-01 19:47:15
问题 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 回答1: 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 !

rlang: Get names from … with colon shortcut in NSE function

醉酒当歌 提交于 2019-12-30 07:06:43
问题 I'm writing a package of functions for making tables of demographics data. I have one function, abbreviated below, where I need to take in several columns ( ... ) on which I'll gather a data frame. The trick is I'd like to keep those columns' names in order, because I'll need to put a column in that order after gathering. In this case, those columns are estimate , moe , share , sharemoe . library(tidyverse) library(rlang) race <- structure(list(region = c("New Haven", "New Haven", "New Haven"

Rename in dplyr 0.7+ function

对着背影说爱祢 提交于 2019-12-29 08:48:29
问题 I am having difficulty renaming columns in a function using dplyr. I have already found helpful posts on nonstandard evaluation and the use of enquo (e.g., http://dplyr.tidyverse.org/articles/programming.html and Changing names of resulting variables in custom dplyr function). The ultimate goal is to use the function to summarise each group, and then rename the columns into something more meaningful than the original variable names. Here is a simple example, which works except for the

Create a new variable using dplyr::mutate and pasting two existing variables for user-defined function

*爱你&永不变心* 提交于 2019-12-24 20:47:13
问题 I would like to create a function to join the lower and higher bound of confidence intervals (named as CIlow and CIhigh ) from a data frame. See the data frame below as example. dataframe<-data.frame(CIlow_a=c(1.1,1.2),CIlow_b=c(2.1,2.2),CIlow_c=c(3.1,3.2), CIhigh_a=c(1.3,1.4),CIhigh_b=c(2.3,2.4),CIhigh_c=c(3.3,3.4)) The data frame has CIlow and CIhigh for a number of groups (named as a , b and c ) and for a number of variables (in this case two, the rows of the data frame). group<-c("a","b",

Referring to individual variables in … with dplyr quos

三世轮回 提交于 2019-12-24 03:04:27
问题 Reading the guide to programming with dplyr, I am able to refer to all ... variables at once. But how can I use them individually? Here's a function that counts two variables. It succeeds using quos() and !!! : library(dplyr) # version 0.6 or higher library(tidyr) # counts two variables my_fun <- function(dat, ...){ cols <- quos(...) dat <- dat %>% count(!!!cols) dat } my_fun(mtcars, cyl, am) #> # A tibble: 6 x 3 #> cyl am n #> <dbl> <dbl> <int> #> 1 4 0 3 #> 2 4 1 8 #> 3 6 0 4 #> 4 6 1 3 #>

enquo() inside a magrittr pipeline

我是研究僧i 提交于 2019-12-23 19:24:50
问题 I just would like to understand what's going wrong here. In the first case (working), I assign the enquo() -ted argument to a variable, in the second case, I use the enquoted argument directly in my call to mutate . library("dplyr") df <- tibble(x = 1:5, y= 1:5, z = 1:5) # works myfun <- function(df, transformation) { my_transformation <- rlang::enquo(transformation) df %>% gather("key","value", x,y,z) %>% mutate(value = UQ(my_transformation)) } myfun(df,exp(value)) # does not work myfun_2 <-

Use dplyr::case_when with arguments programmatically

半世苍凉 提交于 2019-12-20 15:12:07
问题 I'd like to be able to use dplyr 's case_when in a programmatic way to replace base R cut() function. Currently, case_when can be used with an external argument through NSE like : library(dplyr) library(rlang) patterns <- list( x <= 2 ~ "<=2", x <= 4 ~ "2<->4", x > 4 ~ ">4" ) x <- 1:10 case_when(!!!patterns) What I want to do is : use it with another variable, inside a mutate The idea would be something like this, although I can't figure out how to get it to work : library(dplyr) patterns

What does the !! operator mean in R, particularly in the context !!sym(“x”)

…衆ロ難τιáo~ 提交于 2019-12-20 05:15:28
问题 What does "!!" do in R, and why would you use it? Specifically, I'm looking at a function that involves the phrase a = !!sym("x") where "x" is a string. I thought sym worked by turning a string into an object, so a = sym("x") would set a equal to the object x . What is the !! there for? I read that it unquotes whatever is after it, but I thought sym itself unquoted strings? I also see !! used with other functions. What is it doing? 回答1: This is probably a duplicate but since it's the only

Remove columns the tidyeval way

百般思念 提交于 2019-12-18 16:48:40
问题 I would like to remove a vector of columns using dplyr >= 0.7 library(dplyr) data(mtcars) rem_cols <- c("wt", "qsec", "vs", "am", "gear", "carb") head(select(mtcars, !!paste0("-", rem_cols))) Error: Strings must match column names. Unknown columns: -wt, -qsec, -vs, -am, -gear, -carb dplyr < 0.7 worked as follows: head(select_(mtcars, .dots = paste0("-", rem_cols))) # mpg cyl disp hp drat # Mazda RX4 21.0 6 160 110 3.90 # Mazda RX4 Wag 21.0 6 160 110 3.90 # Datsun 710 22.8 4 108 93 3.85 #