Questions tagged [rlang]

rlang is an R package for creating tidy evaluation interfaces and manipulating language and environment objects. It is a utility package underlying many of the tidyverse family of packages.

rlang is an R package for creating tidy evaluation interfaces and manipulating language and environment objects. It is a utility package underlying many of the tidyverse family of packages.

rlang offers tools for building an alternate non-standard evaluation (NSE) interface, redubbed "tidy eval", based on quasiquotation. Its basic operators are quo() for quoting and !! (said "bang-bang") for unquoting. The tidy eval framework is now integrated in many tidyverse packages, including dplyr and tidyr.

786 questions
47
votes
9 answers

Use variable names in functions of dplyr

I want to use variable names as strings in functions of dplyr. See the example below: df <- data.frame( color = c("blue", "black", "blue", "blue", "black"), value = 1:5) filter(df, color == "blue") It works perfectly, but I would like…
kuba
  • 1,005
  • 2
  • 11
  • 16
24
votes
3 answers

How to parametrize function calls in dplyr 0.7?

The release of dplyr 0.7 includes a major overhaul of programming with dplyr. I read this document carefully, and I am trying to understand how it will impact my use of dplyr. Here is a common idiom I use when building reporting and aggregation…
Paul
  • 3,321
  • 1
  • 33
  • 42
23
votes
1 answer

How do {{}} double curly brackets work in dplyr?

I saw Hadley's talk at RConf and he mentioned using double brackets for calling variables in tidy evals. I searched Google but I couldn't find anything talking about when to use them. What's the use case for double brackets in dplyr?
Cauder
  • 2,157
  • 4
  • 30
  • 69
23
votes
1 answer

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

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…
maleta
  • 383
  • 1
  • 2
  • 10
19
votes
7 answers

Defunct as of rlang 0.3.0 and mutate_impl

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.…
eyama
  • 303
  • 2
  • 6
19
votes
1 answer

rlang::sym in anonymous functions

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], …
Jonas
  • 1,639
  • 1
  • 18
  • 29
19
votes
2 answers

dplyr rename - Error: `new_name` = old_name must be a symbol or a string, not formula

I am trying to rename a column with dplyr::rename() and R is returning this error that I am unable to find anywhere online. Error: `new_name` = old_name must be a symbol or a string, not formula Reproducible example with a 2 column data…
18
votes
4 answers

Custom pipe to silence warnings

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))…
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
18
votes
4 answers

Pass column names as strings to group_by and summarize

With dplyr starting version 0.7 the methods ending with underscore such as summarize_ group_by_ are deprecated since we are supposed to use quosures. See: https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html I am trying to…
witek
  • 984
  • 1
  • 8
  • 25
16
votes
5 answers

pass function arguments to both dplyr and ggplot

I'm confused about how to pass function argument into dplyr and ggplot codes. I'm using the newest version of dplyr and ggplot2 Here is my code to produce a barplot (clarity vs mean price) diamond.plot<- function (data, group, metric) { group<-…
zesla
  • 11,155
  • 16
  • 82
  • 147
15
votes
4 answers

How to use dplyr's enquo and quo_name in a function with tidyr and ggplot2

library(dplyr) #Devel version, soon-to-be-released 0.6.0 library(tidyr) library(ggplot2) library(forcats) #for gss_cat data I'm attempting to write a function that combines quosures from the soon-to-be-released dplyr devel version together with…
Mike
  • 2,017
  • 6
  • 26
  • 53
14
votes
3 answers

Why does only `case_when` give different results in R?

I noticed this behavior below when I used dplyr::case_when instead of if in reference to this article. If the output of the second branch is an explicit string, it works as intended, but if the x itself is specified, the result changes. Why does…
ryoto
  • 361
  • 1
  • 10
13
votes
3 answers

Use of tidyeval based non-standard evaluation in recode in right-hand side of mutate

Consider a tibble where each column is a character vector which can take many values -- let's say "A" through "F". library(tidyverse) sample_df <- tibble(q1 = c("A", "B", "C"), q2 = c("B", "B", "A")) I wish to create a function which takes a…
aaron
  • 315
  • 1
  • 7
13
votes
2 answers

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

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…
Ben G
  • 4,148
  • 2
  • 22
  • 42
13
votes
2 answers

dplyr 0.7.0 tidyeval in packages

Preamble I commonly use dplyr in my packages. Prior to 0.7.0, I was using the underscored versions of dplyr verbs to avoid NOTEs during R CMD CHECK. For example, the code: x <- tibble::tibble(v = 1:3, w = 2) y <- dplyr::filter(x, v > w) would have…
user3646834
1
2 3
52 53