I've been trying to find a solution for this for over a year already and decided to write a post about it. Any help would be appreciated. Here is the pseudocode that I can do easily in Stata and SAS but I don't know how to do in R. {} is the glue-like operator that was introduced into dplyr this year, so I'm using it as a placeholder for the operator that makes the pseudocode work.
library(tidyverse)
var <- "mpg"
df_name <- "mtcars"
{df_name} %>% count({var})
{df_name}_1 <- {df_name} %>% mutate(., {var}_1={var}/2)
length({df_name}_1)
should lead to
library(tidyverse)
var <- "mpg"
df_name <- "mtcars"
mtcars %>% count(mpg)
mtcars_1 <- mtcars %>% mutate(., mpg_1=mpg/2)
length(mtcars_1)
In Stata, I can easily do with local or global macros like this:
local df_name "mtcars"
then reference it as `df_name'
In SAS I can do it with global macros like this:
%LET df_name=mtcars;
then reference it like &df_name.
Please note how visually easy it is to reference these values -- no assigns, gets, parentheses, mgets, etc .
Both approaches allow to use them in dataset names, functions, variables, etc. Simplifies my code tremendously and saves me tons of time. How to do this with visual simplicity in R? My code should be readable for people familiar with Stata/SAS (dplyr is awesome in this regard!) and too many evals, wrapping everything in functions, assigns with parentheses will just make them give up on the project or force me to change back to SAS/Stata.
I tried all combinations of {{}}, !!, enquo, sym, and the NSE and still don't know how to make this work in a visually simple way. In dplyr pipes, there is finally some workaround for the variable names but nothing for the dataframes and base R.
I would really appreciate any help in this matter! I had this problem back in 2009 with R and gave up on R until I had to come back in 2019 and still can't find an easy way to approach this.