[R] Using with() to avoid $ ?

Bert Gunter bgunter.4567 at gmail.com
Sun Oct 23 18:18:17 CEST 2016

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

Please correct any misstatements of fact in the above as well as clarifying
anything else I seem confused about.

Many thanks.


	[[alternative HTML version deleted]]

More information about the R-help mailing list