MASS masks dplyr, but not vice versa. Why?

474 views
Skip to first unread message

Bob

unread,
May 16, 2016, 1:50:45 PM5/16/16
to manipulatr
Hi All,

Loading the MASS package after loading dplyr will cause MASS's select function to dominate, as expected. But the reverse is not true. Loading dplyr after MASS doesn't cause dplyr's select function to mask MASS's. How come?

Note there's a related topic here: https://groups.google.com/forum/#!searchin/manipulatr/mass/manipulatr/1d9wMG5vFfU/osZRGLd9_lMJ

What really bugs me is that once Deducer is loaded, it blocks you from detaching MASS, so there's no way to use Deducer's SAS-like frequencies function with dplyr's select without using the form dplyr::select. Since my goal of using the two was to introduce migrating SAS users to the wonders of R, it makes R look particularly confusing on one of the first examples.

Any ideas?  Example below.

TIA,
Bob

> library("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

> mycars <- as_data_frame(mtcars)
> select(mycars, vs:carb)
Source: local data frame [32 x 4]

      vs    am  gear  carb
   (dbl) (dbl) (dbl) (dbl)
1      0     1     4     4
2      0     1     4     4
3      1     1     4     1
4      1     0     3     1
5      0     0     3     2
6      1     0     3     1
7      0     0     3     4
8      1     0     4     2
9      1     0     4     2
10     1     0     4     4
..   ...   ...   ...   ...


> # Here I load MASS, whose "select" function
> # masks the one from dplyr, causing trouble:
> library("MASS")

Attaching package: ‘MASS’

The following object is masked from ‘package:dplyr’:

    select

> select(mycars,vs:carb)
Error in select(mycars, vs:carb) : unused argument (vs:carb)



> # So I thought reloading dplyr would cause
> # its "select" function to mask the one 
> # from MASS, but it does not!
> library("dplyr")
> select(mycars,vs:carb)
Error in select(mycars, vs:carb) : unused argument (vs:carb)


> # Detaching MASS fixes it
> detach("package:MASS")
> select(mycars,vs:carb)
Source: local data frame [32 x 4]

      vs    am  gear  carb
   (dbl) (dbl) (dbl) (dbl)
1      0     1     4     4
2      0     1     4     4
3      1     1     4     1
4      1     0     3     1
5      0     0     3     2
6      1     0     3     1
7      0     0     3     4
8      1     0     4     2
9      1     0     4     2
10     1     0     4     4
..   ...   ...   ...   ...



> # But the Deducer package requires MASS!
> # This analysis is my actual goal.
> options(DeducerNoGUI=TRUE)
> library("Deducer")
Loading required package: ggplot2
Loading required package: JGR
Loading required package: rJava
Loading required package: JavaGD
Loading required package: iplots

Please type JGR() to launch console. Platform specific launchers (.exe and .app) can also be obtained at http://www.rforge.net/JGR/files/.


Loading required package: car
Loading required package: MASS

Attaching package: ‘MASS’

The following object is masked from ‘package:dplyr’:

    select


Loading Deducer without GUI. If you wish the GUI to load, run options(DeducerNoGUI=FALSE) before loading Deducer

> frequencies(
+   select(mycars,vs:carb) )
Error in select(mycars, vs:carb) : unused argument (vs:carb)




> # Now Deducer won't allow MASS to be detached!
> detach("package:MASS")
Error: package ‘MASS’ is required by ‘Deducer’ so will not be detached



> # So the only solution is to use:
> frequencies(
+   dplyr::select(mycars,vs:carb) )
$vs
------------------------------------------------------------
--                        Frequencies                     --
--                                                        --
  Value # of Cases       % Cumulative %
1     0         18    56.2         56.2
2     1         14    43.8        100.0
--                                                        --
--                        Case Summary                    --
--                                                        --
           Valid Missing Total
# of cases    32       0     1
--                                                        --
--                                                        --
------------------------------------------------------------






$am
------------------------------------------------------------
--                        Frequencies                     --
--                                                        --
  Value # of Cases       % Cumulative %
1     0         19    59.4         59.4
2     1         13    40.6        100.0
--                                                        --
--                        Case Summary                    --
--                                                        --
           Valid Missing Total
# of cases    32       0     1
--                                                        --
--                                                        --
------------------------------------------------------------






$gear
------------------------------------------------------------
--                        Frequencies                     --
--                                                        --
  Value # of Cases       % Cumulative %
1     3         15    46.9         46.9
2     4         12    37.5         84.4
3     5          5    15.6        100.0
--                                                        --
--                        Case Summary                    --
--                                                        --
           Valid Missing Total
# of cases    32       0     1
--                                                        --
--                                                        --
------------------------------------------------------------






$carb
------------------------------------------------------------
--                        Frequencies                     --
--                                                        --
  Value # of Cases       % Cumulative %
1     1          7    21.9         21.9
2     2         10    31.2         53.1
3     3          3     9.4         62.5
4     4         10    31.2         93.8
5     6          1     3.1         96.9
6     8          1     3.1        100.0
--                                                        --
--                        Case Summary                    --
--                                                        --
           Valid Missing Total
# of cases    32       0     1
--                                                        --
--                                                        --
------------------------------------------------------------






> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] Deducer_0.7-9 MASS_7.3-45   car_2.1-2     JGR_1.7-16    iplots_1.1-7 
[6] JavaGD_0.6-1  rJava_0.9-8   ggplot2_2.1.0 dplyr_0.4.3  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.5        nloptr_1.0.4       plyr_1.8.3        
 [4] effects_3.1-1      class_7.3-14       tools_3.3.0       
 [7] lme4_1.1-12        gtable_0.2.0       nlme_3.1-128      
[10] lattice_0.20-33    mgcv_1.8-12        png_0.1-7         
[13] Matrix_1.2-6       DBI_0.4-1          parallel_3.3.0    
[16] mvtnorm_1.0-5      SparseM_1.7        e1071_1.6-7       
[19] MatrixModels_0.4-1 grid_3.3.0         nnet_7.3-12       
[22] R6_2.1.2           survival_2.39-4    foreign_0.8-66    
[25] multcomp_1.4-5     TH.data_1.0-7      minqa_1.2.4       
[28] magrittr_1.5       codetools_0.2-14   scales_0.4.0      
[31] splines_3.3.0      assertthat_0.1     pbkrtest_0.4-6    
[34] colorspace_1.2-6   quantreg_5.24      sandwich_2.3-4    
[37] lazyeval_0.1.10    munsell_0.4.3      zoo_1.7-13   

Ista Zahn

unread,
May 16, 2016, 4:59:33 PM5/16/16
to Bob, manipulatr

Hi Bob,

I cannot replicate the problem you describe. If you start a clean R session and run

library(MASS)
library(dplyr)

Will indeed result in dplyr::select masking MASS:select

Read ?library again, especially the detals section where it says that calling library on a package name that is already attached does not reload it, and what to do if you want to reload a package.

Best,
Ista

--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+...@googlegroups.com.
To post to this group, send email to manip...@googlegroups.com.
Visit this group at https://groups.google.com/group/manipulatr.
For more options, visit https://groups.google.com/d/optout.

Steve Lianoglou

unread,
May 16, 2016, 5:11:53 PM5/16/16
to Bob, manipulatr
Hi,

Briefly:

> What really bugs me is that once Deducer is loaded, it blocks you from
> detaching MASS, so there's no way to use Deducer's SAS-like frequencies
> function with dplyr's select without using the form dplyr::select. Since my
> goal of using the two was to introduce migrating SAS users to the wonders of
> R, it makes R look particularly confusing on one of the first examples.

After all of your loading of Deducer is all said-and-done, you could
include this:

select <- dplyr::select

Then your selects will work as you intend (ie. they will be dplyr's
version, and not MASS's). You would, of course, have to use
"MASS::select" if you want to use MASS's version ... or you can create
a new function `mselect <- MASS::select`

HTH,
-steve

Bob

unread,
May 17, 2016, 8:00:02 AM5/17/16
to manipulatr
Ista, thanks for the reference to the library help file. To my surprise, I've been using R for more than a decade thinking that the last package loaded would always dominate, even if I reloaded an earlier package. But that's only because I almost always loaded a second package, used it, and never tried to reload a previously loaded package! It looks like that's not the case unless you add unload=TRUE to detach. 

Thanks,
Bob

Bob

unread,
May 17, 2016, 8:02:55 AM5/17/16
to manipulatr, muen...@tennessee.edu, lianogl...@gene.com
Steve, 

Thanks for the suggestion. Before reading that I was using dplyr::select in the middle of the other call, but I think your suggestion splits that out into a smaller more digestible step for the beginners I'm often working with.

Thanks,
Bob
Reply all
Reply to author
Forward
0 new messages