I am new to package development and using Tidyverse within functions where I need to pass variable names using embrace into the functions. Part of my problem is that I don't fully understand why some of the things I am doing work. I have just been pragmatic and got something working and just moved on.
I understand some of the basics of how to use embraces as part of
dplyr
. I understand how to create new variables using quotes and embraced variables. For example, if I wanted to add one to all the foo
values in a new variable I could do mutate("{{foo}}_new" := {{foo}} + 1)
. I find myself in a function where I need to create a new variable and then depending on the data/conditions within each group, use different functions based on my newly created variable.
In other words, if I create a new column of {{foo}}_new
with a mutate function, how do you use that new variable on the RHS of a mutate function? I have tried several combinations of !!
things and trying to do enquo()
. But honestly, I don't really understand what
In my reprex below, I have some basic x-y data and I am trying to create ordinal x-values by group. I have two functions, the first function ord_by_group
shows the successful calculation of a new variable. The second function manipulate_new_variable
duplicates the first parts of the previous function, but includes my attempt at working with the new variable on the RHS. I fully understand that the function fails because it is trying to subtract 1 from a string of "{{x_val}}_ord"
.
Any help or advice on working with new variables with names based on embraced variables would be appreciated.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
func_dat <- tibble(
x_col = rep(c(1 / 7, 1, 2, 4, 26, 52),2),
y_col = runif(12,0,100),
grp = c(rep(1, 6), rep(2, 6)))
ord_by_group <- function(dat,x_val,...){
#Can get function to grp based on ...
dat = dat %>%
group_by(...) %>%
arrange(...)
#Can create ordinals by group
dat = dat %>%
mutate("{{x_val}}_ord" := row_number())
return(dat)
}
ord_by_group(func_dat,x_col,grp)
#> # A tibble: 12 x 4
#> # Groups: grp [2]
#> x_col y_col grp x_col_ord
#> <dbl> <dbl> <dbl> <int>
#> 1 0.143 71.7 1 1
#> 2 1 79.2 1 2
#> 3 2 36.8 1 3
#> 4 4 26.8 1 4
#> 5 26 45.0 1 5
#> 6 52 16.8 1 6
#> 7 0.143 13.7 2 1
#> 8 1 74.7 2 2
#> 9 2 55.2 2 3
#> 10 4 66.9 2 4
#> 11 26 78.4 2 5
#> 12 52 99.8 2 6
manipulate_new_variable <- function(dat,x_val,...){
#Can get function to grp based on ...
dat = dat %>%
group_by(...) %>%
arrange(...)
#Can create ordinals by group
dat = dat %>%
mutate("{{x_val}}_ord" := row_number())
#How do you use the new variable for future calculations?
dat = dat %>%
mutate(new_var = "{{x_val}}_ord"-1)
return(dat)
}
manipulate_new_variable(func_dat,x_col,grp)
#> Error: Problem with `mutate()` input `new_var`.
#> x non-numeric argument to binary operator
#> i Input `new_var` is `"{{x_val}}_ord" - 1`.
#> i The error occurred in group 1: grp = 1.
Created on 2020-10-20 by the reprex package (v0.3.0)
EDIT: I previously wrote LHS a bunch of times but I meant RHS of a mutate.