Is it possible to wrap an R function to amend its functionality?
Here's a toy example to explain what I mean. Consider this function sum2
:
sum2 <- function (x) if (length(x) == 1) { cat(x); sum(x) } else sum(x)
It does what sum
does, with a tiny modification. Suppose I'd like to redefine sum
itself to do what sum2
does here. How can I do this in a general way, without knowing anything about the internals of the function I'm wrapping?
I would like to do this to temporarily "fix" a package function without having to modify and -reinstall the package. I would like to check for its inputs and return a special value in case the input satisfies some condition.
(For those who are deeply familiar with Mathematica, I'm looking for something similar to the Gayley-Villegas trick.)