[R] Understanding namespace for plyr / dplyr

arun smartpink111 at yahoo.com
Wed Jan 29 23:39:22 CET 2014


Hi,
You can use dplyr:::summarise
For e.g.
library(plyr)
library(dplyr)
> summarise
function (.data, ...) 
----------------------
}
<environment: namespace:plyr>

library(Lahman)
 Batting %.% group_by(playerID) %.% summarise(total=sum(G))%.% head(5)
#    total
#1 4988101
 Batting %.% group_by(playerID) %.% dplyr:::summarise(total=sum(G))%.% head(5)
Source: local data frame [5 x 2]

   playerID total
1 zuverge01   266
2  zupofr01    16
3 zupcibo01   319
4 zumayjo01   171
5 zuberbi01   224
A.K.




I think I have a hole in my understanding of how R uses packages (or at 
least how it gives functions in packages priority).  I thought I would give 
the new dplyr package a test drive this morning (which is blazingly fast 
BTW) and I've gone down the rabbit hole. 

The issue is that I'm unable to use both plyr and dplyr in a program that 
I'm writing.  When I initially install dplyr and I look at the summarise 
function everything works great but if I then install the plyr package the 
summarise function from dplyr is then masked with the plyr summarise 
function taking priority.  I can't seem to figure out a way to get the 
dplyr summarise to become the main function... 

What am I missing here? Can you not use the dplyr and plyr packages at the 
same time? 


Example: 
========================= 
> require(dplyr) 
Loading required package: dplyr 

Attaching package: 'dplyr' 

The following objects are masked from 'package:stats': 

    filter, lag 

The following objects are masked from 'package:base': 

    intersect, setdiff, setequal, union 

> summarise 
function (.data, ...) 
UseMethod("summarise") 
<environment: namespace:dplyr> 
> 
======================= 
***However, if I then install plyr I get what is below and masks the dplyr 
summarise function:** 

> require(plyr) 
Loading required package: plyr 
Attaching package: 'plyr' 
The following objects are masked from 'package:dplyr': 
    arrange, desc, failwith, id, mutate, summarise 
> summarise 
function (.data, ...) 
{ 
    stopifnot(is.data.frame(.data) || is.list(.data) || 
is.environment(.data)) 
.... 
...} 
<environment: namespace:plyr> 
> 
=============== 
** Then no going back... 
require(dplyr) 
> summarise 
function (.data, ...) 
{ 
    stopifnot(is.data.frame(.data) || is.list(.data) || 
is.environment(.data)) 
.... 
...} 
<environment: namespace:plyr> 
>




More information about the R-help mailing list