why slice function not working without explicit use of dplyr

Viewed 4337

I am learning to use dplyr and came across function slice. I tried to call it with and without explicit dplyr.

It works with explicit calling but gives error when called without dplyr. Can someone guide me what is the source of the error?

As far I know slice function is exclusive to dplyr or it belongs to some other package as well?

> library("tidyverse") 
> sessionInfo()

R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.14.1

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

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

other attached packages:
 [1] faraway_1.0.7       pd.hg.u95av2_3.12.0 DBI_1.0.0           oligo_1.46.0        oligoClasses_1.44.0
 [6] RSQLite_2.1.1       Biostrings_2.50.2   XVector_0.22.0      IRanges_2.16.0      S4Vectors_0.20.1   
[11] ArrayExpress_1.42.0 Biobase_2.42.0      BiocGenerics_0.28.0 bindrcpp_0.2.2      forcats_0.3.0      
[16] stringr_1.3.1       dplyr_0.7.8         purrr_0.2.5         tidyr_0.8.2         tibble_2.0.0       
[21] ggplot2_3.1.0       tidyverse_1.2.1     readr_1.3.1        

loaded via a namespace (and not attached):
 [1] nlme_3.1-137                bitops_1.0-6                matrixStats_0.54.0         
 [4] lubridate_1.7.4             bit64_0.9-7                 httr_1.4.0                 
 [7] GenomeInfoDb_1.18.1         tools_3.5.1                 backports_1.1.3            
[10] utf8_1.1.4                  R6_2.3.0                    affyio_1.52.0              
[13] lazyeval_0.2.1              colorspace_1.3-2            withr_2.1.2                
[16] tidyselect_0.2.5            bit_1.1-14                  curl_3.3                   
[19] compiler_3.5.1              preprocessCore_1.44.0       cli_1.0.1                  
[22] rvest_0.3.2                 xml2_1.2.0                  DelayedArray_0.8.0         
[25] labeling_0.3                scales_1.0.0                digest_0.6.18              
[28] minqa_1.2.4                 pkgconfig_2.0.2             lme4_1.1-19                
[31] limma_3.38.3                rlang_0.3.1                 readxl_1.2.0               
[34] rstudioapi_0.9.0            bindr_0.1.1                 generics_0.0.2             
[37] jsonlite_1.6                BiocParallel_1.16.5         RCurl_1.95-4.11            
[40] magrittr_1.5                GenomeInfoDbData_1.2.0      Matrix_1.2-15              
[43] Rcpp_1.0.0                  munsell_0.5.0               fansi_0.4.0                
[46] stringi_1.2.4               yaml_2.2.0                  MASS_7.3-51.1              
[49] SummarizedExperiment_1.12.0 zlibbioc_1.28.0             plyr_1.8.4                 
[52] grid_3.5.1                  affxparser_1.54.0           blob_1.1.1                 
[55] crayon_1.3.4                lattice_0.20-38             haven_2.0.0                
[58] splines_3.5.1               hms_0.4.2                   pillar_1.3.1               
[61] GenomicRanges_1.34.0        codetools_0.2-16            XML_3.98-1.16              
[64] glue_1.3.0                  BiocManager_1.30.4          modelr_0.1.2               
[67] nloptr_1.2.1                foreach_1.4.4               cellranger_1.1.0           
[70] gtable_0.2.0                assertthat_0.2.0            broom_0.5.1                
[73] ff_2.2-14                   iterators_1.0.10            memoise_1.1.0 

> worldcup %>% dplyr::slice(1:3)


Team   Position Time Shots Passes Tackles Saves player_name

1 Algeria Midfielder   16     0      6       0     0      Abdoun

2   Japan Midfielder  351     0    101      14     0         Abe

3  France   Defender  180     0     91       6     0      Abidal

> worldcup %>% slice(1:3)

Error in .Call2("Rle_constructor", values, lengths, PACKAGE = "S4Vectors") :

  Rle of type 'list' is not supported
2 Answers

I think a safer solution might be to use dplyr::slice in case you end up needing S4Vectors for something else. Perhaps another package relies on S4Vectors or even S4Vectors::slice explicitly.

worldcup %>% slice(1:3) calls slice from S4Vectors package and not dplyr.

Try doing:

detach("package:S4Vectors")

Then run:

worldcup %>% slice(1:3)

Related