I went through a lot of questions that are similar to mine but only addressed one part of my problem. I am using dplyr with standard evaluation to accommodate variable names. This works fine for filter_ and group_by_ in a pipe. However, for summarize I cannot have a variable name for the metric I'm summing. An example will make it clear.
library(dplyr)
library(lazyeval)
# create data
a <- data.frame(
x = c(2010, 2010, 2011, 2011, 2011),
y_zm = c(rep(10, 5)),
y_r2 = c(rep(20, 5)))
# define variable names
tag <- "2011"
metric <- "y"
run1 <- "zm"
run2 <- "r2"
# working example for a pipe with fixed variable name
a %>%
filter_(~x == tag) %>%
group_by_(tag) %>%
summarise_(variable_name = interp(~sum(var, na.rm = T),
var = as.name(paste0(metric,"_",run1))))
# non-working example of what I want to do
a %>%
filter_(~x == tag) %>%
group_by_(tag) %>%
summarise_(as.name(paste0(metric,"_",run1)) =
interp(~sum(var, na.rm = T),
var = as.name(paste0(metric,"_",run1))))
I tried a lot of different things involving as.name() or interp() but nothing seems to work.