Re: [R] optional fields in function declarations

From: Adaikalavan Ramasamy <ramasamy_at_cancer.org.uk>
Date: Sun, 20 May 2007 00:21:32 +0100

Can you provide an simple example of what you want the function to do?

Generally, I set some value in the default.

raise <- function(x, power=1){ return( x^power ) }

 > raise(5)
[1] 5
 > raise(5,3)
[1] 125

Or you can do the same but in a slightly unclear manner.

raise <- function(x, power){

   if(missing(power)) power <- 1
   return( x^power )
}

I prefer the former.

Regards, Adai

claudio.is_at_libero.it wrote:
> Dear R users,
>
> I need to create a set of function to solve some tasks. I want to leave the operator to decide whether uses default parameters or change it; so the functions may have some optional fields. I tied to use the function missing(), but it will work properly only if the optional field is decleared at last in the function.
> Can you give me some suggestion an some reference?
>
> thank you.
>
>
> Claudio
>
>
> ------------------------------------------------------
> Passa a Infostrada. ADSL e Telefono senza limiti e senza canone Telecom
> http://click.libero.it/infostrada
>
> ______________________________________________
> R-help_at_stat.math.ethz.ch 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.
>
>
>



R-help_at_stat.math.ethz.ch 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. Received on Sat 19 May 2007 - 23:27:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 20 May 2007 - 10:31:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.