-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WISH: Way to call function with explicit "missing" arguments #17
Comments
Here is the discussion from the old wiki entry (not sure if it's still relevant):
|
Following up on our Twitter conversation, I use my_sample <- function(x, size) {
## capture original call
mc <- match.call()
## update size (if it's available)
if (!is.null(mc$size)) mc$size <- mc$size * 2
## update call to use base::sample
mc[[1]] <- quote(sample)
## evaluate the updated call
eval(mc, envir = parent.frame())
} I think manipulating the calls is a very powerful R feature and I really enjoy doing that. Eg you can come up with a set of functions with the my_head <- my_tail <- function(x, n) {
## capture original call
mc <- match.call()
## update size (if it's available)
if (!is.null(mc$n)) mc$n <- mc$n * 2
## update call to use base::sample
mc[[1]] <- as.symbol(sub('^my_', '', deparse(mc[[1]])))
## evaluate the updated call
eval(mc, envir = parent.frame())
} |
Maybe my Even if I'd still argue that my_function <- function(x, size=NULL) {
if (!is.null(size)) size <- 2*size
y <- sample(x, size=size)
mean(y)
} is much more readable etc. |
Here's a print.MISSING_VALUE <- function(x, ...) cat("<MISSING VALUE>\n")
str.MISSING_VALUE <- function(x, ...) cat(" <MISSING VALUE>\n")
as.character.MISSING_VALUE <- function(x, ...) character(0L)
missing <- local({
UNIQUE_VALUE <- .Machine$integer.max-1L
MISSING_VALUE <- structure(UNIQUE_VALUE, class=c("MISSING_VALUE"))
function(x) {
if (base::missing(x)) return(MISSING_VALUE)
expr <- substitute(base::missing(x), list(x=substitute(x)))
if (eval(expr, envir=parent.frame())) return(TRUE)
if (identical(x, MISSING_VALUE)) return(TRUE)
FALSE
}
}) Examples
|
Wouldn't a simpler fix be to make |
@hadley, yes, this needs to be implemented by R itself internally (i.e. in C). Hopefully just in the code for setting up and executing function calls (whereever that is located?). My example in the comment was just to further illustrate the idea with a "runnable" example (I've update to "
This comment is a bit too sparse for me; is it referring to my mockup example or are you suggesting how |
You could implement it yourself in the same way that lazyeval does (to improve |
one more approach not listed here is to pass my_sample <- function(x, size) {
eval(call('sample', x, if(missing(size)) substitute() else size*2))
} in such simple example it isn't much useful. |
This can be: my_sample <- function(x, size) {
if (!missing(size)) {
size <- size * 2
}
sample(x, size)
} |
(adopted from Wiki entry)
Wish
A way to specify that an argument value should be considered "missing", e.g.
foo(x, y=missing())
.Background / Issue
Some functions use code that is evaluated conditionally on an argument being (explicitly) specified or not - if not explicitly specified we say the argument is "missing" . For example,
base::sample()
setssize
to bex
orlength(x)
iff "missing" (depending on the value ofx
);If explicit "missing" values would be supported by R, we could do things like:
Instead, we have to write:
Comment
A common design pattern is to allow
NULL
to represent a "missing" value;Note that this would allow us to do:
See also
The text was updated successfully, but these errors were encountered: