possible improvement to ?with examples

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

possible improvement to ?with examples

bbolker
A querent on StackOverflow asked about the with() function

http://stackoverflow.com/questions/42283479/why-when-to-use-with-function#42283479

and asked about the example in ?with

library(MASS)
     with(anorexia, {
         anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),
                         family = gaussian)
         summary(anorex.1)
     })

which saves little or no typing relative to

   anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),
                         family = gaussian, data=anorexia)

(I would argue that the latter is better practice anyway).

  Could we have something more sensible like

   with(mtcars,mpg[cyl==8 & disp>350])

?  (It could be contrasted directly with

mtcars$mpg[mtcars$cyl==8 & mtcars$disp>350]

)

I'm happy to submit a bug report/patch if that seems appropriate.

  cheers
    Ben Bolker

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

Re: possible improvement to ?with examples

Martin Maechler
>>>>> Ben Bolker <[hidden email]>
>>>>>     on Thu, 16 Feb 2017 15:37:13 -0500 writes:

    > A querent on StackOverflow asked about the with() function
    > http://stackoverflow.com/questions/42283479/why-when-to-use-with-function#42283479

    > and asked about the example in ?with

    > library(MASS)
    > with(anorexia, {
    >  anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),
    >                 family = gaussian)
    >  summary(anorex.1)
    > })

    > which saves little or no typing relative to

    > anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),
    > family = gaussian, data=anorexia)

    > (I would argue that the latter is better practice anyway).

    > Could we have something more sensible like

    > with(mtcars,mpg[cyl==8 & disp>350])

    > ?  (It could be contrasted directly with

    > mtcars$mpg[mtcars$cyl==8 & mtcars$disp>350]

    > )

I now have done something like the above, and have added a
\note{ .. }  to warn about "over - use" of with().

Also added a link to Thomas Lumley's paper
  Thomas Lumley (2003)  \emph{Standard nonstandard evaluation rules}.
  \url{http://developer.r-project.org/nonstandard-eval.pdf}

    > I'm happy to submit a bug report/patch if that seems appropriate.

Thank you, Ben!
Martin

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