dgpb
dgpb

Reputation: 419

match.arg function for list argument

My question is simple.

x=list(type="call")

FUN <- function(x=list(type=c("call","put")))
{
  x$type=match.arg(x$type)
}

This returns an error:

> FUN(x)
Error in match.arg(x$type) : 'arg' should be one of “”

Any ideas?

Upvotes: 0

Views: 3950

Answers (1)

Matthew Lundberg
Matthew Lundberg

Reputation: 42659

Perhaps this is what you want:

FUN <- function(x=list(type=c("call","put")))
{
  x$type=match.arg(x$type, c('call', 'put'))
}


> print(FUN())
[1] "call"
> print(FUN(x))
[1] "call"
> print(FUN(list(type="put")))
[1] "put"

Upvotes: 2

Related Questions