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
0
votes
3 answers
How to define the RHS of case_when() with existing and newly defined columns passed as arguments in user defined function?
Sample code:
library(tidyverse)
iris <- iris
test_tidyeval <- function(data, col_name, col_name_2, column1) {
mutate(
data,
{{col_name}} := case_when(Species == "setosa" ~ column1 + Sepal.Width + Petal.Length,
TRUE ~…

Nautica
- 2,004
- 1
- 12
- 35
0
votes
1 answer
Tidyeval and if-statement in ggplot wrapper
I am trying to make a ggplot2 wrapper that adjusts the scale on the y-axis depending on the scale of y.
plot_1: gives an error
plot_2: with tidyeval, but the output has no
values on the y-axis
plot_3: this is how it should look
Any idea what…

tricktracktriu
- 3
- 1
0
votes
0 answers
Using dplyr inside a function with an argument named like a column in dataframe
Could someone explain why f1 behaves differently than f2 in this example:
library(dplyr)
f1 <- function(data, year){
data %>%
filter(year == year)
}
f2 <- function(data, y){
data %>%
filter(year == y)
}
f3 <- function(data, year){
…

Phil
- 33
- 4
0
votes
0 answers
Custom function does not work properly unless the object is stored in the global environment in R
Context
I have a custom function myfun1 that fits the cox model. Before fitting the model, I need to do a bit of processing on the data used to fit the model. Specifically, run two lines of code, dd = datadist(data) and options(datadist = 'dd').
If…

zhiwei li
- 1,635
- 8
- 26
0
votes
2 answers
inject() function from rlang package cannot work with Predict() function from rms package in R
Context
I am learning use tidy eval to write my own function.
If I calculate hr1 without using tidy eval, the code works fine, such as:
hr1 = Predict(fit, meal.cal = q, fun = exp, ref.zero = TRUE)
When I use rlang::inject() and rms::Predict()…

zhiwei li
- 1,635
- 8
- 26
0
votes
2 answers
Function using sym() and deparse(substitute()) not working as expected
I'm trying to build a function that takes two sorts of inputs, either numeric or character, changes them or leaves them as they are given class, then filters a dataframe by those arguments.
library(tidyverse)
fun1 =…

Dasr
- 777
- 6
- 16
0
votes
2 answers
How can I use tidy evaluation in this example?
I havea tibble which looks like
library(tidyverse)
d <- tibble(
y1 = 10:12,
y2 = 3:5,
n1 = rep(100, 3),
n2 = rep(100, 3)
)
and a function which operates on each of these four columns
f <- function(y1, n1, y2, n2){
log(y1/n2) -…

Demetri Pananos
- 6,770
- 9
- 42
- 73
0
votes
1 answer
Best tidyverse practice for passing column names as variables in function
I am passing a tibble to a user-defined function where column names are variables. After studying this, this, and this, I came up with the below working function. My goal is to include an equivalent function in an R package. My question, while this…

greengrass62
- 968
- 7
- 19
0
votes
2 answers
Return object vector (e.g. list of variables) as character vector in R
I would like to return an object vector (e.g. a list of variables) as a character vector in R. This is as close as I've gotten:
library(tidyverse)
data <- as_tibble(mtcars)
foo <- function (vars) {
as.character(enexpr(vars))
}
foo(c(hp, drat,…

chukpl
- 1
- 1
0
votes
1 answer
use variable in str_detect's first argument
I want to use the str_detectfunction passing a variable as the first argument. Meaning this could theoretically look something like this.
# create the variable
var = names(mtcars)[1]
mtcars %>%
mutate(
new_var = case_when(str_detect(var,…

Lenn
- 1,283
- 7
- 20
0
votes
0 answers
use variable (which contains the column-index) in dplyr select
When I have a variable like:
col_to_select = 2
And I want to use the value from that variable in my dplyr::select, how would I do that?
mtcars %>%
select(col_to_select)
gives me obviously this error:
Error in `select()`:
! Can't subset columns…

Lenn
- 1,283
- 7
- 20
0
votes
1 answer
can not understand Curly Curly in tidyeavl in r
I used {{}} to bulid a self-function in r.
I don't understand why this happend.
test.data = data.frame(pm10 = 1:5)
test.data
new.col.name = 'lag_pm10'
col = 'pm10'
test.data %>% # error
mutate({{new.col.name}} := lag({{col}}, 2))
test.data %>%…

zhiwei li
- 1,635
- 8
- 26
0
votes
1 answer
R how to wrap a function that uses tidyeval?
I have a function that uses tidyeval like select_this defined below. I have another function wrapper_select_this wrapping it, but I get the below error. Is there a simple way to adjust the wrapper_select_this to work (in this case: output the same…

its.me.adam
- 333
- 2
- 11
0
votes
1 answer
Create a function with dplyr with two env-variables
I am trying to write functions when there are two env-variables. This vignette has multiple examples with one env variable and multiple data variables, but no examples with two env variables.
https://dplyr.tidyverse.org/articles/programming.html
I…

user2738483
- 147
- 1
- 2
- 11
0
votes
1 answer
R for loop going wrong when applied to function
I am trying to work on a for loop to make running a function I've developed more efficient.
However, when I put it in a for loop, it is overwriting columns that it should not be and returning incorrect results.
Edit: The error is that in the…

Diego
- 23
- 6