Skip to content

Instantly share code, notes, and snippets.

@flodel
Last active January 19, 2019 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flodel/5283216 to your computer and use it in GitHub Desktop.
Save flodel/5283216 to your computer and use it in GitHub Desktop.
ok.comma <- function(FUN) {
function(...) {
arg.list <- as.list(sys.call())[-1L]
len <- length(arg.list)
if (len > 1L) {
last <- arg.list[[len]]
if (missing(last)) {
arg.list <- arg.list[-len]
}
}
do.call(FUN, arg.list)
}
}
@RLogik
Copy link

RLogik commented Jan 19, 2019

If one creates ones own function and uses ok.comma as it is defined to replace a function definition, eg f <- ok.comma(f), then it leads to an infinite loop. Furthermore if you call an ok.comma-d function under your definition, variables will always be read from .GlobalEnv. This is clearly undesirable. Two slight modifications resolves this issue:

ok.comma <- function(f) {
    FUN <- f;
    return(function(...) {
        args <- as.list(sys.call())[-1L];
        e <- parent.frame();
        len <- length(args);
        if(len > 1L) {
            last <- args[[len]];
            if(missing(last)) args <- args[-len];
        }
        do.call(FUN, args, envir=e);
    });
};

In my GitHub package devtools::install_github('RLogik/rbettersyntax', dep=TRUE); I provide this modification along with a few other methods. Credit is given to you for this method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment