Skip to content
Back to formatted view

Raw Message

Message-ID: <CAAyVsXKhLgabuUjUtYtWhVkFL6L6ud8dXe5fNTS8pZqkCH7ZrQ@mail.gmail.com>
Date: 2017-09-08T23:55:01Z
From: Axel Urbiz
Subject: quote()/eval() question

Dear list,

For a reason it would take me long to explain, I need to do something along
the lines of what's shown below -- i.e., create an object from
dplyr::summarise, and then evaluate it on a data frame.

I know I could directly do:

 df %>% dplyr::summarise(x1_mean = mean(x1))

but this is not what I'm looking for.


library(dplyr)

df <- data.frame(x1 = rnorm(100), x2 = rnorm(100))

foo <- function(df) {

  mySummary <- quote(dplyr::summarise(x1_mean = mean(x1)))

  df %>% eval(mySummary)

}

foo(df)

Error in eval(., mySummary) : invalid 'envir' argument of type 'language'

Thank you,
Axel.

	[[alternative HTML version deleted]]