Suppose I replace a package function, for example knitr:::sub_ext . (Note: I am particularly interested in where this is an internal function, i.e. only ::: , not :: , but the same answer may work for both).
library(knitr) my.sub_ext <- function (x, ext) { return("I'm in your package stealing your functions D:") }
Question: is there a way to get the original knitr:::sub_ext after I have done this? Preferably without reloading the package?
(I know that some people want to know why I would like to do this, that's all. No reading is required for the question). I fixed some functions in such packages (not really sub_ext function ...):
original.sub_ext <- knitr:::sub_ext new.sub_ext <- function (x, ext) {
I agree that this is not a good idea at all (in most cases, these are quick fixes until the changes turn into CRAN, or they are โfeature requestsโ that will never be approved, as they are case-specific).
The problem with the above is that I accidentally execute it twice (for example, at the top of the script, which I run twice without restarting R in between), the second time original.sub_ext is actually the previous new.sub_ext as opposed to the real one knitr:::sub_ext , so I get infinite recursion.
Since sub_ext is an internal function (I would not call it directly, but functions from knitr like knit all called it internally), I cannot hope to change all the functions that call sub_ext , call new.sub_ext manually, therefore, the approach of replacing the definition in package namespace.