Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent a library from masking functions

Tags:

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.
like image 512
H. Shindoh Avatar asked Oct 15 '16 21:10

H. Shindoh


2 Answers

The next version of R has this in the NEWS{.Rd} file (quoted from the NEWS file post-build):

• The import() namespace directive now accepts an argument except
  which names symbols to exclude from the imports. The except
  expression should evaluate to a character vector (after
  substituting symbols for strings). See Writing R Extensions.

There referenced text from the manual is here (in raw texi format).

So soon we can. Right now one cannot, and that is a huge pain in the aRse particular when functions from Base R packages are being masked: lag(), filter(), ...

We have used the term anti-social for this behaviour in the past. I don't think it is too strong.

To illustrate the problem, here is a snippet of code I wrote a decade ago (and had it posted on the now-vanished R Graph Gallery) which uses a clever and fast way to compute a moving average:

  ## create a (normalised, but that's just candy) weight vector
  weights <- rep(1/ndays, ndays)
  ## and apply it as a one-sided moving average calculations, see help(filter)
  bbmiddle <- as.vector(filter(dat$Close, weights,
                               method="convolution", side=1))

If you do library(dplyr) as you might in an interactive session, you're dead in the water as filter() is now something completely different. Not nice.

like image 78
Dirk Eddelbuettel Avatar answered Oct 17 '22 19:10

Dirk Eddelbuettel


  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).

like image 29
Dason Avatar answered Oct 17 '22 18:10

Dason