How to deal with functions masked by dplyr?

353 views
Skip to first unread message

Ross Gayler

unread,
Jan 7, 2015, 6:07:09 PM1/7/15
to manip...@googlegroups.com
Hi,

dplyr::collapse masks nlme::collapse, so I have to load dplyr after nlme in order for dplyr to work as expected.

Similarly, dplyr::select masks MASS::select, so I have to load dplyr after nlme in order for dplyr to work as expected.

I presume that the consequence of loading dplyr last is that the masked functions no longer work as expected (unless the dplyr function is a strict superset of the masked function. Given that I loaded the earlier-loaded packages (e.g. nlme and MASS) because I needed to use them, how do I use them with dplyr loaded subsequently?

Obviously, I can just call the masked functions as nlme::collapse or MASS::select, but what happens to calls to masked functions from the code within the earlier loaded package? Possibly more difficult, what about calls to masked function from other packages? For example mgcv depends on nlme, so if mgcv calls nlme::collapse and I wan to use dplyr, what should the load order be: nlme > mgcv > dplyr?

More generally, what are the potential problems when functions are masked? How do you deal with those problems? Are there any problems that can't be dealt with?

I expect this has already been dealt with somewhere. A pointer would be greatly appreciated.

Thanks

Ross

Hadley Wickham

unread,
Jan 7, 2015, 6:12:35 PM1/7/15
to Ross Gayler, manipulatr
> Obviously, I can just call the masked functions as nlme::collapse or
> MASS::select, but what happens to calls to masked functions from the code
> within the earlier loaded package?

Nothing - see http://r-pkgs.had.co.nz/namespace.html for an example
and explanation.

Hadley

--
http://had.co.nz/

Ross Gayler

unread,
Jan 7, 2015, 6:32:08 PM1/7/15
to manip...@googlegroups.com
Thanks for the pointer Hadley. Without having yet read your namespace document (because it will take me a while to read properly and that doesn't guarantee I'll have properly understood it):

Your response refers to what happens to calls to a masked function from within the same package. (Everything works as expected. Bewdy!)

What happens for a cross-package call? For example, mgcv depends on nlme and loads it if it isn't already loaded. Let's assume that mgcv calls 'collapse' (expected to be in nlme and I'll assume the calls from mgcv are to 'collapse' rather than explicitly to 'nlme::collapse'). If I subsequently load dplyr, which masks collapse in nlme, will calls to collapse from mgcv go to dplyr::collapse or nlme::collapse?

Thanks

Ross

Ross Gayler

unread,
Jan 7, 2015, 9:55:58 PM1/7/15
to manip...@googlegroups.com
Final update after an off-line exchange with Hadley:

Assuming the packages are well written, the package load order has no impact on function calls from within the packages. The only impact of load order is on the destination of unqualified calls from the run time environment.

Reply all
Reply to author
Forward
0 new messages