[R] How to set an argument such that a function treats it as missing?
Michael Bedward
michael.bedward at gmail.com
Sat Nov 13 10:24:48 CET 2010
Hello Marius,
NULL is not the same as missing. You could something like this in
various ways. Here are a couple...
g <- function(x) {
if (missing(x)) {
f()
} else {
f(x)
}
}
or change f to detect null args
g <- function(x) {
if (missing(x)) {
x <- NULL
}
f(x)
}
f <- function(x) {
if (missing(x) | is.null(x)) {
// do something
}
}
Michael
On 13 November 2010 19:14, Marius Hofert <m_hofert at web.de> wrote:
> 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?)
> ______________________________________________
> R-help at 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.
>
More information about the R-help
mailing list