Hadley Wickham
2013-Feb-15 15:45 UTC
[Rd] Printing of anonymous functions in calls is sub-optimal
e.g. substitute(f(x), list(f = function(x) x + 1)) # function (x) # x + 1(x) An extra pair of parentheses would really help: (function(x) x + 1)(x) (Better indenting etc would be nice, but not necessary for correct understand of the code) Hadley -- Chief Scientist, RStudio http://had.co.nz/
Bert Gunter
2013-Feb-16 06:24 UTC
[Rd] Printing of anonymous functions in calls is sub-optimal
As there has been no response to this ... Why not simply:> g <- substitute(f(x),list(f=function(x){x+1})) ## with curly braces > gfunction (x) { x + 1 }(x)> x <- 2 > eval(g)[1] 3 Cheers, Bert On Fri, Feb 15, 2013 at 7:45 AM, Hadley Wickham <h.wickham at gmail.com> wrote:> e.g. > > substitute(f(x), list(f = function(x) x + 1)) > # function (x) > # x + 1(x) > > An extra pair of parentheses would really help: > > (function(x) > x + 1)(x) > > (Better indenting etc would be nice, but not necessary for correct > understand of the code) > > Hadley > > -- > Chief Scientist, RStudio > http://had.co.nz/ > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Duncan Murdoch
2013-Feb-16 15:18 UTC
[Rd] Printing of anonymous functions in calls is sub-optimal
On 13-02-15 10:45 AM, Hadley Wickham wrote:> e.g. > > substitute(f(x), list(f = function(x) x + 1)) > # function (x) > # x + 1(x) > > An extra pair of parentheses would really help: > > (function(x) > x + 1)(x) > > (Better indenting etc would be nice, but not necessary for correct > understand of the code)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. Duncan