[R] with vs. attach

Spencer Graves spencer.graves at effectivedefense.org
Fri May 6 11:37:00 CEST 2016



On 5/5/2016 11:17 PM, Bert Gunter wrote:
> ... and it's exactly with.default's code !


Thanks for pointing that out.  Unfortunately, it didn't work inside 
another function.  However, if I had looked at it, I might have been 
able to thought to try it.  Spencer
>
> Cheers,
> Bert
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Thu, May 5, 2016 at 7:38 PM, Spencer Graves
> <spencer.graves at effectivedefense.org> wrote:
>> Hi, David:  That works.  Thanks very much.  Spencer Graves
>>
>>
>>
>> On 5/5/2016 7:43 PM, David Winsemius wrote:
>>>> On May 5, 2016, at 5:12 PM, Spencer Graves
>>>> <spencer.graves at effectivedefense.org> wrote:
>>>>
>>>> I want a function to evaluate one argument
>>>> in the environment of a data.frame supplied
>>>> as another argument.  "attach" works for
>>>> this, but "with" does not.  Is there a way
>>>> to make "with" work?  I'd rather not attach
>>>> the data.frame.
>>>>
>>>>
>>>> With the following two functions "eval.w.attach"
>>>> works but "eval.w.with" fails:
>>>>
>>>>
>>>> dat <- data.frame(a=1:2)
>>>> eval.w.attach <- function(x, dat){
>>>>     attach(dat)
>>>>     X <- x
>>>>     detach()
>>>>     X
>>>> }
>>>>
>>>> eval.w.with <- function(x, dat){
>>>>     with(dat, x)
>>>> }
>>>>
>>>> eval.w.attach(a/2, dat) # returns c(.5, 1)
>>> How about using eval( substitute( ...))?
>>>
>>>    eval.w.sub <- function(expr, datt){
>>>      eval( substitute(expr), env=datt)
>>>                            }
>>>    eval.w.sub(a/2, dat)
>>> #[1] 0.5 1.0
>>>
>>>
>> ______________________________________________
>> R-help at r-project.org 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.



More information about the R-help mailing list