From leydold at statmath.wu.ac.at  Mon May 24 15:44:42 2010
From: leydold at statmath.wu.ac.at (Josef Leydold)
Date: Mon, 24 May 2010 15:44:42 +0200
Subject: [Rd] possible bug in formals
In-Reply-To: <alpine.LFD.2.00.1005231547290.376@gannet.stats.ox.ac.uk>
References: <mailman.13.1251540004.31955.r-devel@r-project.org>
	<20100523141455.GA17210@zwirn.stat-math.wu.ac.at>
	<4BF93991.5090809@statistik.tu-dortmund.de>
	<alpine.LFD.2.00.1005231547290.376@gannet.stats.ox.ac.uk>
Message-ID: <20100524134442.GA18978@zwirn.stat-math.wu.ac.at>

Dear Brian and Uwe,

Thanks a lot for the clarification.
I made the naive assumption that numeric constants in R are similar to
those in C. 

Two questions still remain:

(1) when I have a function 

    f<- function(a=1,b=-1) { a+b }

    is it safe to use 

    val <- as.character(deparse(formals(f)$b))

    to obtain a string that contains the default value for
    argument "b". (Does is also work for other arguments with some 
    default of arbitrary class?)

(2) I have seen that packages like gWidget (in function ggenericwidget)
    use a statement like

    switch(class(formals(f)$b),
	numeric = { .... },
        character = { .... },
        class = { .... }, ....

    for automatically processing function arguments.
    in the case of "b=-1" this procedure obviously fails.
    (I found this behavior of 'formals' while playing around with 
    packages "gWidgets" and "fgui" from CRAN).

    Is there a safe workaround for this problem?

    That is, is there a safe function that returns class
    "numeric" for an exresion like "-1" or "-Inf"?

Josef


On Sun, May 23, 2010 at 03:52:00PM +0100, Prof Brian Ripley wrote:
> Documented too: from ?NumericConstants
> 
>       Note that a leading plus or minus is not regarded by the parser as
>       part of a numeric constant but as a unary operator applied to the
>       constant.
> 
> 
> 
> On Sun, 23 May 2010, Uwe Ligges wrote:
> 
> >
> >
> > On 23.05.2010 16:14, Josef Leydold wrote:
> >> Hi,
> >> 
> >> I am a little bit surprised by the following output of
> >> 'formals'. Is this the intended behavior?
> >> 
> >>> f<- function(a=1,b=-1) { a+b }
> >>> class(formals(f)$a)
> >> [1] "numeric"
> >>> class(formals(f)$b)
> >> [1] "call"
> >> 
> >> 
> >> Josef
> >> 
> >> 
> >
> >
> > Yes, the arguments have not yet been evaluated, hence -1 is still a language 
> > object.
> >
> > Try to write
> > f<- function(a= +1, b= -1) { a+b }
> > and you will find that this is a fascinating feature.
> >
> > Uwe Ligges
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
> 
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
> 

-- 


-----------------------------------------------------------------------------
Josef Leydold   |  WU (Vienna University of Economics and Business)
                |  Institute for Statistics and Mathematics
-----------------------------------------------------------------------------
Augasse 2-6     |  Tel.   +43 1 31336 4695
A-1090 Vienna   |  FAX    +43 1 31336 774
European Union  |  email  josef.leydold at wu.ac.at
-----------------------------------------------------------------------------
Alles Unglueck kam daher, dass die Denkenden nicht mehr handeln konnten,
und die Handelnden keine Zeit mehr fanden zu denken.       (Marlen Haushofer)



[Rd] possible bug in formals
Josef Leydold