0

I'm creating a custom function to make charting data easier but am running into issues with the processing phase. I've checked the below link and many others but it does not answer my question:

Error: unexpected '}' in "}"

When running the below function, I continue to get the following error: Error: unexpected '}' in "}":

 test<-function(df, kpi){
   df<- df %>% mutate(week= lubridate::floor_date(date, unit= 'week', week_start = 1))
   trended<- df %>% 
   pivot_longer(cols= -c('date', 'week'), names_to = 'metric', values_to= 'value') %>% 
   filter(str_detect(metric, kpi)) %>%
   group_by(week) %>%
   summarise(!!kpi= mean(value))
  return(trended)
}

I'm trying to use the name of the kpi to name the new variable that I am averaging. I know the function is fine because the below version runs without issue, but when I try to use !! or as.name(), I get the above error. See clean version without a variable name and dummy data below:

test<-function(df, kpi){
  df<- df %>% mutate(week= lubridate::floor_date(date, unit= 'week', week_start = 1))
   trended<- df %>% 
   pivot_longer(cols= -c('date', 'week'), names_to = 'metric', values_to= 'value') %>% 
   filter(str_detect(metric, kpi)) %>%
   group_by(week) %>%
   summarise(mean(value))
 return(trended)
}

df<- tibble(date= seq.Date(from= as.Date('2021-01-01'), to= as.Date('2021-01-31'), by = 1), x= (1:31), y= (31:1))
Konrad Rudolph
  • 506,650
  • 124
  • 909
  • 1,183
dre
  • 464
  • 3
  • 16
  • 1
    I am getting a different error: “Error: unexpected '=' in: " group_by(week) %>% summarise(!!kpi="” — And that’s entirely correct: you need to use `:=` instead of `=` with computed argument names in tidyeval. – Konrad Rudolph Nov 21 '21 at 15:57

0 Answers0