H. Shindoh H. Shindoh - 1 month ago 11
R Question

How can I prevent a library from masking functions in R

A typical situation is the following:

library(dplyr)
library(xgboost)


When I import the library
xgboost
, the function
slice
of
dplyr
is masked, and I have to write
dplyr::slice
even though I never use
xgboost::slice
explicitly.

The obvious solution to the problem is to import
xgboost
before
dplyr
. But it is crazy to import all libraries which can affect the functions of
dplyr
in advance. Moreover this problem often happens when I use
caret
library. Namely
train
function imports automatically required libraries and some functions are masked at the time.


  1. It is possible to prevent some functions from being masked?

  2. Is it possible to mask "the masking function" (e.g.
    xgboost::slice
    ) with an early imported function (e.g.
    dplyr::slice
    )?



Notes


  • I am NOT asking how to disable warning message.

  • I am NOT asking how to use the masked functions.


Answer
  1. It is possible to prevent some functions from being masked?

I don't believe so but I could be wrong. I'm not sure what this would look like

  1. Is it possible to mask "the masking function" (e.g. xgboost::slice) with an early imported function (e.g. dplyr::slice)?

If you're asking about just or use in an interactive session you can always just define slice to be the function you actually want to use like so

slice <- dplyr::slice

and then you can use slice as if it is the dplyr version (because now it is).

Comments