[Rd] Printing of anonymous functions in calls is sub-optimal

William Dunlap wdunlap at tibco.com
Sun Feb 17 06:47:45 CET 2013


> I suspect it's only when you have a function in the quoted call, not a symbol:

Add a call to 'function' (as opposed to the function made by evaluating that call)
to your test suite:

  > Q <- list(
         q1 = quote(getFunction("-")(x)),
         q2 = substitute(f(x), list(f = function(x) {-x})),
         q3 = substitute(f(x), list(f = quote(function(x) {-x}))))
  > sapply(Q, function(x)class(x[[1]]))
          q1         q2         q3 
      "call" "function"     "call" 
  > Q
  $q1
  getFunction("-")(x)
  
  $q2
  function (x) 
  {
      -x
  }(x)

  $q3
  function(x) {
      -x
  }(x)

  > sapply(Q, eval, list(x=137))
    q1   q2   q3 
  -137 -137 -137

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf
> Of Hadley Wickham
> Sent: Saturday, February 16, 2013 7:22 AM
> To: Duncan Murdoch
> Cc: r-devel at r-project.org
> Subject: Re: [Rd] Printing of anonymous functions in calls is sub-optimal
> 
> > This is a little tricky for the deparser.  It sees a call to a function
> > which was determined by an expression.  Sometimes you want parens, sometimes
> > you don't.  For example, if getfun(y) returns a function, it's clearer to
> > display a call as getfun(y)(x) than (getfun(y))(x).
> >
> > I'll see if I can work out which kinds of expressions need to be
> > parenthesized and implement it in the deparser.
> 
> I suspect it's only when you have a function in the quoted call, not a symbol:
> 
> # Don't add parens
> q1 <- quote(g(f)(x))
> is.symbol(q1[[1]])
> 
> # Add parents
> q2 <- substitute(f(x), list(f = function(x) {x + 1}))
> is.function(q2[[1]])
> 
> Thanks for thinking about it!
> 
> Hadley
> 
> --
> Chief Scientist, RStudio
> http://had.co.nz/
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list