R - how to make base "merge" function the default one

Viewed 110

I'm quite an R newbie, so apologizes if mine is a simple question. I have many R scripts that use the base merge function. Yesterday, I had to install the config library and that completely screwed up the merge function since it's also defined inside congif. I read this useful post: R - can't merge dataframe after installing config package but my question here is different. I can't go through tens of R scripts and replace "merge()" with "base::merge()".

So my very simple question is the following and very similar to what discussed here: R: 2 functions with the same name in 2 different packages

how can I specify to use the "base::" version of the function "merge()" as default one? Or - is there a way to not install the "merge()" function contained inside the config package? Many thanks

1 Answers

The accepted answer to R - can't merge data frame after installing config package directs the user to explicitly reference functions from the config package as config::merge() or config::get().

Another way to address the problem of config masking base::merge() is to load the config package, use it to configure the environment, and then use detach() to remove the package. This will unmask base::merge().

library(config)
# use config functions to set up environment
#

At this point we can show that config::merge is the default by printing the merge() function.

> merge
function (base_config, merge_config) 
{
    merge_lists(base_config, merge_config, recursive = TRUE)
}
<bytecode: 0x7fcddf5de488>
<environment: namespace:config>
>

To restore base::merge() as the default, we use the detach() function.

detach(package:config)
# at this point base::merge() and base::get() are unmasked

To demonstrate this, we'll print the merge() function again.

> detach(package:config)
> # print merge function to show it is from base package 
> merge
function (x, y, ...) 
UseMethod("merge")
<bytecode: 0x7fcde7c08e70>
<environment: namespace:base>
> 
Related