2

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

Angelo
  • 1,594
  • 5
  • 17
  • 50
  • 1
    If you're only using a few functions from config, you might be best not to load it, but to call the function explicitly with "config::function_name()" – Miff Apr 03 '20 at 12:38
  • 1
    The accepted answer to the second linked question seems to offer an alternative solution, after `library(config)` do `merge <- base::merge` – Miff Apr 03 '20 at 12:44
  • 10s of scripts doesn't seem like that many. You can do a global find/replace in less than 10 minutes. – Edward Apr 03 '20 at 12:53
  • you're right guys, find/replace is not a big deal - but I was scared because on the server we have hundreds and more of R script so I didn't want to replace other people's code. The solution posted below by Len is actually fixing my specific problem, thanks to the use of the detach function. Much appreciated your help. – Angelo Apr 03 '20 at 13:37

1 Answers1

1

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>
> 
Len Greski
  • 10,505
  • 2
  • 22
  • 33
  • Thank you Len, you're right, if I detach it after I use it, then merge() is not "masked" by the merge function defined in config. Very very useful. Thank you – Angelo Apr 03 '20 at 13:36
  • @Angelo - You're welcome, Angelo. My answer to [What is the difference between the workspace and the environments?](https://stackoverflow.com/questions/60962116/what-is-the-difference-between-the-workspace-and-the-environments/60962508#60962508) illustrates how loading a package inserts it between the Global Environment and the environment of the most recently loaded package. Since the `base` environment is second to last in the environment search path, any package loaded in an R session that includes functions with the same names as ones in `base` will mask the functions from `base`. – Len Greski Apr 03 '20 at 14:22