| 1 |
wrap <- function(f, pre, post, envir = parent.frame()) { |
| 2 |
fmls <- formals(f) |
| 3 |
|
| 4 |
# called pass all extra formals on |
| 5 |
called_fmls <- setNames(lapply(names(fmls), as.symbol), names(fmls)) |
| 6 |
|
| 7 |
f_call <- as.call(c(substitute(f), called_fmls)) |
| 8 |
pre <- substitute(pre) |
| 9 |
post <- substitute(post) |
| 10 |
|
| 11 |
fun <- eval(bquote( |
| 12 |
function(args) { |
| 13 |
.(pre) |
| 14 |
.retval <- .(f_call) |
| 15 |
.(post) |
| 16 |
}, |
| 17 |
as.environment(list(f_call = f_call, pre = pre, post = post)) |
| 18 |
)) |
| 19 |
|
| 20 |
# substitute does not work on arguments, so we need to fix them manually |
| 21 |
formals(fun) <- fmls |
| 22 |
|
| 23 |
environment(fun) <- envir |
| 24 |
|
| 25 |
fun |
| 26 |
} |