quote()/eval() question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

quote()/eval() question

Axel Urbiz
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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: quote()/eval() question

Duncan Murdoch-2
On 08/09/2017 7:55 PM, Axel Urbiz wrote:

> 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)

magrittr pipes are just syntactic sugar.  What your second line does is
the same as

eval(df, mySummary)

which makes no sense.  These would work:

eval(df, expr = mySummary)
eval(mySummary, envir = df)

You could write the first as

df %>% eval(expr = mySummary)

and the second as

df %>% eval(mySummary, envir = .)

Duncan Murdoch

>
> }
>
> foo(df)
>
> Error in eval(., mySummary) : invalid 'envir' argument of type 'language'
>
> Thank you,
> Axel.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: quote()/eval() question

David Winsemius
In reply to this post by Axel Urbiz

> On Sep 8, 2017, at 4:55 PM, Axel Urbiz <[hidden email]> wrote:
>
> 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'

You might get a more informative error message if you didn't use `df` as your local variable. It's the name of a function.

--
David.

>
> Thank you,
> Axel.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.