[R] Using with() to avoid $ ?

Duncan Murdoch murdoch.duncan at gmail.com
Mon Oct 24 00:22:46 CEST 2016


On 23/10/2016 3:43 PM, Bert Gunter wrote:
> Yes, variables in the formula should be handled by nse with the data
> argument. Got it -- thanks. But still ... can with() be used to handle
> those and/or any other variables in foo that appear as arguments. I see no
> problems in doing so, but ... ?

One possible problem:

Formulas aren't just the bits you can see, they have environments 
attached.  The NSE part of evaluating them makes sure that the 
environment contains the variables in the data argument early in the chain.

This means that your function can pass the evaluated formula to another 
function, and it will carry the appropriate data with it.

If you evaluate a formula within with(), things will get complicated. 
The environment attached to the formula in

with(foo, f( y ~ x1 + x2 + x3 + ... , data = foo,  ...))

will likely contain two different copies of the variables in foo.  The 
first will be the usual one described above.  But since formulas can 
refer to things in the environment that called f(), it is added to the 
chain of environments that are the parent of the first one.

Environments are reference objects, so f() could decide to modify some 
of the variables.  It would likely get very confused if there were two 
copies of them, one in one environment, one in another.

So I'd advise to use one form or the other, i.e. don't use with(), or if 
you do, don't use data=.

Duncan Murdoch

>
> Bert
>
> (But see inline below)
>
> On Oct 23, 2016 7:24 PM, "Jeff Newmiller" <jdnewmil at dcn.davis.ca.us> wrote:
>>
>> No. And I don't know why you are conflating the treatment of variables in
> the formula with treatment of variables passed as other arguments. It is
> sort of like thinking the x symbols in foo$x[ x < 0 ] refer to the same
> data.
>
> In my query they explicitly do, though. Nevertheless your response was
> apropos.
>
>>
>> foo$y ~ foo$x1 + foo$x2 + foo$x3 is not preferable, and given the
> availability of a data argument such redundancy is unnecessary. NSE is
> already in use for the formula. It is not (necessarily) in use for the
> other arguments, so you just have to learn which arguments are being
> handled with NSE by any particular function and which are not... good docs
> would be the preferred avenue but recognizing the error message that arises
> when you fail to specify foo$ for the non-formula arguments gets me by if
> the docs are unclear.
>>
>> However, it is dangerous to apply NSE tricks recursively, so piling
> "with" on top of the existing formula eval-with-data is only likely to
> confuse the evaluation context even more.
>
> This is what I'm not sure of. Can you give an example of when such
> confusion would occur?
>
>
>>
>> --
>> Sent from my phone. Please excuse my brevity.
>>
>> On October 23, 2016 9:18:17 AM PDT, Bert Gunter <bgunter.4567 at gmail.com>
> wrote:
>>> As has been noted oftimes on this list
>>> f( y ~ x1 + x2 + x3 + ... , data = foo,  ...)
>>>
>>> is much preferable to
>>> f( foo$y ~ foo$x1 + foo$x2 + foo$x3 + ...,  ...)
>>>
>>> (with no data argument), using nse = non-standard evaluation to set the
>>> environment for formula evaluation. However, as queries here recently
>>> demonstrate,  the formula variables (y, x1, x2, x3, ...) or other
>>> variables
>>> in foo are also sometimes needed as further arguments of f,  and these
>>> have
>>> to be explicitly and tediously given as foo$whatever or equivalent
>>> indexing.
>>>
>>> So my question is, can/should with() be used instead in the form
>>> with(foo, f( y ~ x1 + x2 + x3 + ... , data = foo,  ...))  with no
>>> explicit
>>> $ or indexing in ... variables?
>>>
>>> or even
>>> with(foo, f( y ~ x1 + x2 + x3 + ... ,  ...))
>>>
>>> with no data argument for nse or indexing, though this seems to me
>>> questionable in that it may affect the formula's  environment
>>> differently.(??)
>>>
>>> Please correct any misstatements of fact in the above as well as
>>> clarifying
>>> anything else I seem confused about.
>>>
>>> Many thanks.
>>>
>>> Bert
>>>
>>>       [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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