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.
I do not see that needing to use
get
in R in place of needing to use LET in SA is any more difficult. Thedplyr
mechanism for turning named character values into R names or "language"-expressions is the!!
operator, so this seems to be an exact implementation of the first part of your request:You will need to use an
assign
operation for the next parts, though. I don't think dplyr has gone the route of trying to emulate the macro character of SAS which most experienced R users would consider as an abomination. There was an effort about 10 years ago on the part of one of the R-greats to illustrate how R could do macro-processing, but I think he did it more as to show why it should not be done than with any intent to make R more SAS-like. (I'll now go out and look for the article.)Here's the document. It is closer to 20 years ago. Go to page 11.