From murdoch@dunc@n @end|ng |rom gm@||@com  Wed Jul 12 02:24:14 2023
From: murdoch@dunc@n @end|ng |rom gm@||@com (Duncan Murdoch)
Date: Tue, 11 Jul 2023 20:24:14 -0400
Subject: [Rd] question about an R idiom: eval()ing a quoted block
In-Reply-To: <a777a5e6-7363-35ed-3f30-ea526937cdff@gmail.com>
References: <a777a5e6-7363-35ed-3f30-ea526937cdff@gmail.com>
Message-ID: <d7998a5d-8b43-9373-dfd7-b1145dffa8af@gmail.com>

On 11/07/2023 6:01 p.m., Ben Bolker wrote:
>     In a few places in the R source code, such as the $initialize element
> of `family` objects, and in the body of power.t.test() (possibly other
> power.* functions), sets of instructions that will need to be run later
> are encapsulated by saving them as an expression and later applying
> eval(), rather than as a function. This seems weird to me; the only
> reason I can think of for doing it this way is to avoid having to pass
> back multiple objects and assign them in the calling environment (since
> R doesn't have a particularly nice form of Python's tuple-unpacking idiom).
> 
>     Am I missing something?
> 
>    cheers
>      Ben
> 
> 
> https://github.com/r-devel/r-svn/blob/eac72e66a4d2c2aba50867bd80643b978febf5a3/src/library/stats/R/power.R#L38-L52
> 
> https://github.com/r-devel/r-svn/blob/master/src/library/stats/R/family.R#L166-L171

Those examples are very old (the second is at least 20 years old).  It 
may be they were written by someone who was thinking in S rather than in 
R.

As far as I recall (but I might be wrong), S didn't have the same 
scoping rules for accessing and modifying local variables in a function 
from a nested function.

Duncan Murdoch



[Rd] question about an R idiom: eval()ing a quoted block
Duncan Murdoch