replicate evaluates its second argument in wrong environment

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

replicate evaluates its second argument in wrong environment

Gabor Grothendieck
Currently replicate used within sapply within a function can fail
because it gets the environment for its second argument, which is
currently hard coded to be the parent frame, wrong.  See this link for
a full example of how it goes wrong and how it could be made to work
if it were possible to pass an envir argument to it.

https://stackoverflow.com/questions/66184446/sapplya-replicate-b-expression-no-longer-works-inside-a-function/66185079#66185079

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: replicate evaluates its second argument in wrong environment

hadley wickham
This is a nice example of the motivation for tidy evaluation — since
enquo() captures the environment in which the promise should be
evaluated, there's no need for an additional explicit argument.

library(rlang)

replicate2 <- function (n, expr, simplify = "array") {
  exnr <- enquo(expr)
  sapply(integer(n), function(i) eval_tidy(expr), simplify = simplify)
}

doRep2 <- function(a, b) sapply(a, replicate2, b)
doRep2(3, 2)
#>      [,1]
#> [1,]    2
#> [2,]    2
#> [3,]    2

Hadley

On Sat, Feb 13, 2021 at 7:09 AM Gabor Grothendieck
<[hidden email]> wrote:

>
> Currently replicate used within sapply within a function can fail
> because it gets the environment for its second argument, which is
> currently hard coded to be the parent frame, wrong.  See this link for
> a full example of how it goes wrong and how it could be made to work
> if it were possible to pass an envir argument to it.
>
> https://stackoverflow.com/questions/66184446/sapplya-replicate-b-expression-no-longer-works-inside-a-function/66185079#66185079
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



--
http://hadley.nz

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: replicate evaluates its second argument in wrong environment

David Winsemius

On 2/15/21 1:10 PM, Hadley Wickham wrote:
> This is a nice example of the motivation for tidy evaluation — since
> enquo() captures the environment in which the promise should be
> evaluated, there's no need for an additional explicit argument.
>
> library(rlang)
>
> replicate2 <- function (n, expr, simplify = "array") {
>    exnr <- enquo(expr)

It does not appear that the line above would accomplish anything given
the succeeding line. Or am I missing something? Taking it out doesn't
seem to affect results. Whatever magic there is seems to be in the
`eval_tidy` function, whose mechanism or rules seem opaque. Was "exnr"
supposed to be passed to `eval_tidy`?

--

David.

>    sapply(integer(n), function(i) eval_tidy(expr), simplify = simplify)
> }
>
> doRep2 <- function(a, b) sapply(a, replicate2, b)
> doRep2(3, 2)
> #>      [,1]
> #> [1,]    2
> #> [2,]    2
> #> [3,]    2
>
> Hadley
>
> On Sat, Feb 13, 2021 at 7:09 AM Gabor Grothendieck
> <[hidden email]> wrote:
>> Currently replicate used within sapply within a function can fail
>> because it gets the environment for its second argument, which is
>> currently hard coded to be the parent frame, wrong.  See this link for
>> a full example of how it goes wrong and how it could be made to work
>> if it were possible to pass an envir argument to it.
>>
>> https://stackoverflow.com/questions/66184446/sapplya-replicate-b-expression-no-longer-works-inside-a-function/66185079#66185079
>>
>> --
>> Statistics & Software Consulting
>> GKX Group, GKX Associates Inc.
>> tel: 1-877-GKX-GROUP
>> email: ggrothendieck at gmail.com
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: replicate evaluates its second argument in wrong environment

hadley wickham
On Monday, February 15, 2021, David Winsemius <[hidden email]>
wrote:

>
> On 2/15/21 1:10 PM, Hadley Wickham wrote:
>
>> This is a nice example of the motivation for tidy evaluation — since
>> enquo() captures the environment in which the promise should be
>> evaluated, there's no need for an additional explicit argument.
>>
>> library(rlang)
>>
>> replicate2 <- function (n, expr, simplify = "array") {
>>    exnr <- enquo(expr)
>>
>
> It does not appear that the line above would accomplish anything given the
> succeeding line. Or am I missing something? Taking it out doesn't seem to
> affect results. Whatever magic there is seems to be in the `eval_tidy`
> function, whose mechanism or rules seem opaque. Was "exnr" supposed to be
> passed to `eval_tidy`?
>
>
Oops, yes, obviously that was supposed to be expr. It doesn’t matter in
Gabor’s example because it evaluates to a constant but obviously would
matter in other cases.

Hadley


> --
>
> David.
>
>    sapply(integer(n), function(i) eval_tidy(expr), simplify = simplify)
>> }
>>
>> doRep2 <- function(a, b) sapply(a, replicate2, b)
>> doRep2(3, 2)
>> #>      [,1]
>> #> [1,]    2
>> #> [2,]    2
>> #> [3,]    2
>>
>> Hadley
>>
>> On Sat, Feb 13, 2021 at 7:09 AM Gabor Grothendieck
>> <[hidden email]> wrote:
>>
>>> Currently replicate used within sapply within a function can fail
>>> because it gets the environment for its second argument, which is
>>> currently hard coded to be the parent frame, wrong.  See this link for
>>> a full example of how it goes wrong and how it could be made to work
>>> if it were possible to pass an envir argument to it.
>>>
>>> https://stackoverflow.com/questions/66184446/sapplya-replica
>>> te-b-expression-no-longer-works-inside-a-function/66185079#66185079
>>>
>>> --
>>> Statistics & Software Consulting
>>> GKX Group, GKX Associates Inc.
>>> tel: 1-877-GKX-GROUP
>>> email: ggrothendieck at gmail.com
>>>
>>> ______________________________________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>
>>
>>
>>

--
http://hadley.nz

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel