[R] sub question

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue Feb 3 15:03:29 CET 2009


Gabor Grothendieck wrote:
> This comes from the all.vars function and would indicate
> a bug in that base R function.
>
>   

hush!  a user bug, i presume?  but indeed,

all.vars(expression(foo(bar)()))
# character(0)
all.names(expression(foo(bar)()))
# "foo" "bar"

vQ

>> f = function(a) function() paste(a, a, sep="")
>> all.vars(~ fo(o)())
>>     
> character(0)
>
>
> On Tue, Feb 3, 2009 at 8:24 AM, Wacek Kusnierczyk
> <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
>   
>> Peter Dalgaard wrote:
>>     
>>> Gabor Grothendieck wrote:
>>>       
>>>> On Sat, Jan 31, 2009 at 6:01 PM, Wacek Kusnierczyk
>>>>         
>>> th some additional boring pedantry wrt. ?gsubfn, which says:
>>>       
>>>>> " If 'replacement' is a formula instead of a function then a one
>>>>>     line function is created whose body is the right hand side of the
>>>>>     formula and whose arguments are the left hand side separated by
>>>>>     '+' signs (or any other valid operator).  The environment of the
>>>>>     function is the environment of the formula.  If the arguments are
>>>>>     omitted then the free variables found on the right hand side are
>>>>>     used in the order encountered.  "
>>>>>
>>>>> to my little mind, all of 'paste', 'rep', 'nchar', and 'x' in the
>>>>> example above are *free variables* on the right of the formula.  you
>>>>>           
>>>> The first three are functions, not variables.
>>>>         
>>> They are still free variables, subject to the same rules of variable
>>> lookup. Wacek is right: The RHS is scanned recursively for objects of
>>> mode "name" _except_ when they appear as function names (i.e. if
>>> subexpression e is mode "call", then forget e[[1]] and look at the
>>> arguments in as.list(e)[-1]. Not sure if this also happens if e[[1]]
>>> is not a name, e.g. in f(a)(b), do you get both a and b or just b?)
>>>
>>>       
>> an interesting point.  the two calls to gsubfn below should, in this
>> particular case, be equivalent:
>>
>> library(gsubfn)
>>
>> f = function(a) function(b) paste(a, b, sep="")
>> gsubfn('o', ~ f('o')(o), 'foo')
>> # "foooo"
>> gsubfn('o', ~ f(o)('o'), 'foo')
>> # the match seems to be ignored in the formula?
>> the following fails, too:
>>
>> f = function(a) function() paste(a, a, sep="")
>> gsubfn('o', ~ f(o)(), 'foo')
>> # o won't capture the match
>>
>> this as well, though it's rather different:
>>
>> f = function() 'oo'
>> gsubfn('o', ~ f(), 'foo')
>> # really can't ignore the matched pattern if a formula is given?
>>
>>
>> while an average statistician may never write such rubbish code, these
>> are trivialized examples, and for a language advertised as one from the
>> functional family this sort of code is not so unusual and it may be
>> surprising that it fails.
>>     
>
> Can you clarify this.  In what way was the "match ignored"?
> In the first case it added an o after each o.  Were you expecting
> something different?
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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