Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing/de-registering a specific function from an R package

I may not be using the terminology correctly here so please forgive me...

I have a case of one package 'overwriting' a function with the same name loaded by another package and thus changing the behavior (breaking) of a function.

The specific case:

X <- data.frame ( y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100) )

library(CausalImpact)
a <- CausalImpact::CausalImpact( X, c(1,75), c(76, 100) ) # works

library(bfast) # imports quantmod which loads crappy version of as.zoo.data.frame

b <- CausalImpact::CausalImpact( X, c(1,75), c(76, 100) ) # Error

I know the error comes from two versions of the function as.zoo.data.frame. The problematic version is imported by bfast from the package 'quantmod' (see https://github.com/joshuaulrich/quantmod/issues/168). Unfortunately their hotfix did not prevent this error. Super annoying.

I can hack around this specific problem, but I was wondering if there is a general way to like 'de-register' this function variant from the search path. Neither detach nor unloadNamespace remove the offending function (same behavior after). An explanation and similar problem is discussed here and here, but I wasn't able to find a general solution. For instance I'd rather just remove this function than clone and re-write CausalImpact to deal with this behavior.

like image 602
user3357177 Avatar asked Nov 06 '25 05:11

user3357177


2 Answers

From R 3.6.0 onwards, there is a new option called "conflicts.policy" to handle this within an established framework. For small issues like this, you can use the new arguments to library(). If you aren't yet to 3.6, the easiest solution might be to explicitly namespace CausalImpact when you need it, i.e. CausalImpact::CausalImpact. That's a mouthful, so you could do causal_impact <- CausalImpact::CausalImpact and use that alias.

# only attach select
library(dplyr, include.only = "select")
# exclude slice/arrange from being attached.
library(dplyr, exclude = c("slice", "arrange")) 

library(bfast, exclude = "CausalImpact") should solve your problem.

Attach means that they are available for use without explicit prefixing with their package. In either of these cases, something like dplyr::slice would work just fine.

For more information, you can see ?library. Also, the R-Core member Luke Tierney wrote a blog explaining how the conflicts.policy works. You can find that here

like image 125
smingerson Avatar answered Nov 07 '25 19:11

smingerson


Here's an answer that works, but is less preferable than de-registering a S3 method because it involves replacing the registered version in the S3 Methods table with the desired method:

library(CausalImpact)
library(bfast) 
assignInNamespace("as.zoo.data.frame", zoo:::as.zoo.data.frame, ns = asNamespace("zoo"))

based partially on @smingerson's suggestion in the comments

like image 40
user3357177 Avatar answered Nov 07 '25 20:11

user3357177



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!