On Mon, 24 May 2010, Josef Leydold wrote:

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?)

But the defualt value is not character, so this cannot in general be done.


(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"?

Why are you using class() when you seem to mean typeof()?

But the short answer is you appear to be trying to circumvent the language, and who really cares what the default is? If it is used, it is evaluated, and then you can simply do typeof(b). And if it is not used, who cares what it is?


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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


--
Brian D. Ripley,                  rip...@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.leyd...@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)



--
Brian D. Ripley,                  rip...@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

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to