Skip to content

Instantly share code, notes, and snippets.

@t-kalinowski
Last active January 14, 2020 12:58
Show Gist options
  • Save t-kalinowski/ae1880621afef9a07fe82e7cf367fc51 to your computer and use it in GitHub Desktop.
Save t-kalinowski/ae1880621afef9a07fe82e7cf367fc51 to your computer and use it in GitHub Desktop.
`%(%` <- function(fn, args) {
fn <- substitute(fn)
args <- as.list(substitute(args))
if(!identical(args[[1]], quote(`{`)))
stop("right hand side must be an expressionlist starting with a `{`")
args[[1L]] <- NULL
nms <- names2(args)
for(i in seq_along(args)) {
ar <- args[[i]]
if(is.call(ar) && identical(ar[[1L]], quote(`=`))) {
if(!is.symbol(nm <- ar[[2L]])) stop("Left hand side of `=` must be named")
nms[i] <- as.character(nm)
args[i] <- ar[3L]
}
}
names(args) <- nms
if (is.call(fn) && is.symbol(fn[[1L]]) &&
grepl("^%.+%$", as.character(fn[[1L]]))) {
fn[[3L]] <- as.call(c(fn[[3L]], args))
return(eval.parent(fn))
}
# else fn is a symbol or a function meant to be called normally
eval.parent(as.call(c(fn, args)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment