Skip to content
Prev 243672 / 398513 Next

default arguments and '...' in a function

Antonio,

You need to compare the names of list(...) with the arguments you wish to
check. Here's one way to do so (note that I replaced c with d, because c
is a function):

f <- function(a, ...) {
	argnames <- names(list(...))
	# check whether b is an argument
	if(!("b" %in% argnames)) {
		b <- 1
	}
	# check whether d is an argument
	if(!("d" %in% argnames)) {
		d <- 1
	}
        # return NA for b and d if specified, but don't set a value
	list(a=a, b=ifelse(exists("b"), b, NA), d=ifelse(exists("d"), d, NA),
args=list(...))
}
$a
[1] 1

$b
[1] 1

$d
[1] 1

$args
list()
$a
[1] 1

$b
[1] NA

$d
[1] 1

$args
$args$b
[1] 2
$a
[1] 1

$b
[1] NA

$d
[1] NA

$args
$args$b
[1] 2

$args$d
[1] 3

Sarah
On Wed, Dec 1, 2010 at 2:34 PM, <Antonio.Gasparrini at lshtm.ac.uk> wrote: