[R] How to set an argument such that a function treats it as missing?

Marius Hofert m_hofert at web.de
Sat Nov 13 09:14:08 CET 2010


Dear expeRts,

I would like to call a function f from a function g with or without an argument. 
I use missing() to check if the argument is given. If it is not given, can I set 
it to anything such that the following function call (to f) behaves as if the argument
isn't given? It's probably best described by a minimal example (see below).

The reason why I want to do this is, that I do not have to distinguish between the
cases when the argument is given or not. By setting it to something (what?) in the
latter case, I can use the same code in the subsequent part of the function.

Cheers,

Marius



f <- function(x) if(missing(x)) print("f: missing x") else print(x)

g <- function(x){
	if(missing(x)){
		print("g: missing x")
		x <- NULL # I try to set it to something here such that...
	}
	f(x) # ... this call to f behaves like f() 
} 

g() # should print "f: missing x" (is this possible?)


More information about the R-help mailing list