Marius Hofert
2010-Nov-13 08:14 UTC
[R] How to set an argument such that a function treats it as missing?
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?)
Michael Bedward
2010-Nov-13 09:24 UTC
[R] How to set an argument such that a function treats it as missing?
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. >
Gabor Grothendieck
2010-Nov-13 11:29 UTC
[R] How to set an argument such that a function treats it as missing?
On Sat, Nov 13, 2010 at 3:14 AM, 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. >You can pass missing values: f <- function(x) g(x) g <- function(x) missing(x) f(3) # FALSE f() # TRUE -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Duncan Murdoch
2010-Nov-13 11:41 UTC
[R] How to set an argument such that a function treats it as missing?
Marius Hofert 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...Just leave out the line above, and you'll get both messages printed: > g() [1] "g: missing x" [1] "f: missing x" Duncan Murdoch> } > 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.
Seemingly Similar Threads
- How to create an array of lists of multiple components?
- lattice: strange behavior (?) when using trellis.device(color=FALSE)
- How to use expression(italic()) in a "vectorized" way (within a function)?
- How to integrate a function with additional argument being a vector or matrix?
- splom, plotmath: how to add three lines of information with alignment?