claudio.is at libero.it
2007-May-19 18:15 UTC
[R] optional fields in function declarations
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
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.
>
>
>