Tal Galili
2014-Feb-19 21:09 UTC
[R] Package development: overriding a function from one package with a function from another?
Dear R users, I am currently working on developing two packages, below is a simplified version of my problem: In package A I have some functions (say "sum_twice"), and I it calls to another function inside the package (say "slow_sum"). However, in package B, I wrote another function (say "fast_sum"), with which I wish to replace the slow function in package A. Now, how do I manage this "overriding" of the "slow_sum" function with the "fast_sum" function? Here is a simplified example of such functions (just to illustrate): ############################ ############## # Functions in package A slow_sum <- function(x) { sum_x <- 0 for(i in seq_along(x)) sum_x <- sum_x + x[i] sum_x } sum_twice <- function(x) { x2 <- rep(x,2) slow_sum(x2) } ############## # A function in package B fast_sum <- function(x) { sum(x) } ############################ If I only do something like: slow_sum <- fast_sum This would not work, since "sum_twice" uses "slow_sum" from the NAMESPACE of package A. I tried using the following function when loading package "B": assignInNamespace(x = "slow_sum", value = B:::fast_sum, ns = "A") This indeed works, however, it makes the CRAN checks return both a NOTE on how I should not use ":::", and also a warning for using assignInNamespace (since it is supposed to not be very safe). However, I am at a loss. What would be a way to have "sum_twice" use "fast_sum" instead of "slow_sum"? Thank you upfront for any feedback or suggestion, With regards, Tal ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- [[alternative HTML version deleted]]