Please use this tag for questions about using tidy evaluation within the tidyverse framework. For more information please refer to this handbook: https://tidyeval.tidyverse.org/
Questions tagged [tidyeval]
473 questions
54
votes
5 answers
Looping over variables in ggplot
I want to use ggplot to loop over several columns to create multiple plots, but using the placeholder in the for loop changes the behavior of ggplot.
If I have this:
t <- data.frame(w = c(1, 2, 3, 4), x = c(23,45,23, 34),
y = c(23,34,54, 23), z =…

Tom
- 4,860
- 7
- 43
- 55
52
votes
4 answers
dplyr: How to use group_by inside a function?
I want to use use the dplyr::group_by function inside another function, but I do not know how to pass the arguments to this function.
Can someone provide a working example?
library(dplyr)
data(iris)
iris %.% group_by(Species) %.% summarise(n = n())…

Emilio Torres Manzanera
- 5,202
- 2
- 15
- 8
36
votes
4 answers
Why is enquo + !! preferable to substitute + eval
In the following example, why should we favour using f1 over f2? Is it more efficient in some sense? For someone used to base R, it seems more natural to use the "substitute + eval" option.
library(dplyr)
d = data.frame(x = 1:5,
y =…

mbiron
- 3,933
- 1
- 14
- 16
22
votes
3 answers
no visible global function definition for ':='
I am writing a package that uses tidyeval. Because I use tidyeval I have rlang listed under imports in the description file.
One of the functions contains a few lines that use :=
Like this:
data %>%
dplyr::mutate(
!!New_R := AP_R_X*!!X +…

Steen Harsted
- 1,802
- 2
- 21
- 34
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
17
votes
1 answer
dplyr / tidyevaluation: How to pass an expression in mutate as a string?
I want to write a function that has two inputs: The name of a new variable and a mathematical expression. Both arguments come as strings.
This function should take a data.frame and add the specified new variable which should be the result of the…

der_grund
- 1,898
- 20
- 36
17
votes
3 answers
Error when using dplyr inside of a function
I'm trying to put together a function that creates a subset from my original data frame, and then uses dplyr's SELECT and MUTATE to give me the number of large/small entries, based on the sum of the width and length of sepals/petals.
filter <-…

ari8888
- 309
- 1
- 2
- 10
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
2 answers
Why does !! (bang-bang) combined with as.name() give a different output compared to !! or as.name() alone?
I use a dynamic variable (eg. ID) as a way to reference a column name that will change depending on which gene I am processing at the time. I then use case_when within mutate to create a new column that will have values that depend on the dynamic…

Yuka Takemon
- 320
- 1
- 11
14
votes
2 answers
How to combine ggplot and dplyr into a function?
Consider this simple example
library(dplyr)
library(ggplot2)
dataframe <- data_frame(id = c(1,2,3,4),
group = c('a','b','c','c'),
value = c(200,400,120,300))
# A tibble: 4 x 3
id group value
…

ℕʘʘḆḽḘ
- 18,566
- 34
- 128
- 235
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
12
votes
4 answers
What is the difference between . and .data?
I'm trying to develop a deeper understanding of using the dot (".") with dplyr and using the .data pronoun with dplyr. The code I was writing that motivated this post, looked something like this:
cat_table <- tibble(
variable =…

Brad Cannell
- 3,020
- 2
- 23
- 39