Thanks to all who responded.

I've learn a lot from your comments. The best solution I can see is to use both 
'missing()' and
'match.arg()' in my function.

Héctor

> Hi,
>
> I'm sure this one is very easy....
>
> I am trying to write a function where one of its arguments has two posible 
> (strings) values,
> defaulting to one of them if none is specified. My problem is that when 
> evaluating the function
> the following warning is produced:
>
> "the condition has length > 1 and only the first element will be used"
>
> I've read the help page of if() but I don't get it because when evaluating 
> step by step the
> warning is not produced.
>
>
> Omitting the details of my function, below is an example of this
>
> ## Function
> fun <- function( n, result = c("simple", "complete") )
> {
>   if ( is.null(result) )
>      result <-  "simple"
>   if ( !(result %in% c("simple", "s", "complete", "c")) ) {
>       stop("specify type of result 'simple' or 'complete'")
>  } else {
>   res <- rnorm(n)
>   res
>  }
> }
>
> ## This gives the warning
> fun(n=20)
>
> ## But this not
> fun(n=20, result="other")
>
> ## The warning is not produced when doing this step by step
>   n <- 20
>   result <-  "simple"
>   if ( !(result %in% c("simple", "s", "complete", "c")) ){
>       stop("specify type of result 'simple' or 'complete'")
>  } else {
>   res <- rnorm(n)
>   res
>  }
>
> ## or here
>   n <- 20
>   result <-  "other"
>   if ( !(result %in% c("simple", "s", "complete", "c")) ){
>       stop("specify type of result 'simple' or 'complete'")
>  } else {
>   res <- rnorm(n)
>   res
>  }
>
>
> Thanks for your time
>
> Héctor


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to