Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Side effects of the conversion of count() to a generic #5737

Closed
davidski opened this issue Feb 4, 2021 · 1 comment
Closed

Side effects of the conversion of count() to a generic #5737

davidski opened this issue Feb 4, 2021 · 1 comment

Comments

@davidski
Copy link

@davidski davidski commented Feb 4, 2021

This is an extension off of a conversation from the RStudio Community - https://community.rstudio.com/t/subtle-difference-on-rownames-between-two-count-ed-tibbles/95090

#5538 implemented count() as a generic. With this change, dplyr >= 1.0.3 is exhibiting some odd (to me) behavior in regards to rownames and tibbles. It appears that tibbles that are passed in to count() are returned with some different attributes that cause rownames to be printed. If the output of count() is explicitly cast as a tibble, rownames are not printed (as expected). Strangely, the results of these two operations pass a base identical() test but fail an identical(attrib.as.set = FALSE) test, although I don't see any difference in the results of attributes() call on both of the resulting objects. It also appears that the bad object in my reprex returns a true from tibble::has_rownames(), this exists even if I explicitly pass a rownames = FALSE to my as_tibble() call.

The net result of this is that some objects in an analysis workflow are displayed in an html_notebook with rownames and others are not. There may be other side effects that I'm not aware of here.

Expected behavior: bare tibbles as generated by count() are printed without rownames


library(dplyr, warn.conflicts = FALSE)
bad <- mtcars %>% tibble::as_tibble() %>% count(disp)
good <- mtcars %>% count(disp) %>% tibble::as_tibble()
identical(bad, good, attrib.as.set = FALSE)
#> [1] FALSE
bad
#> # A tibble: 27 x 2
#>     disp     n
#>  * <dbl> <int>
#>  1  71.1     1
#>  2  75.7     1
#>  3  78.7     1
#>  4  79       1
#>  5  95.1     1
#>  6 108       1
#>  7 120.      1
#>  8 120.      1
#>  9 121       1
#> 10 141.      1
#> # … with 17 more rows

Created on 2021-02-04 by the reprex package (v1.0.0)

Session info
sessionInfo()
#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS Big Sur 10.16
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices datasets  utils     methods   base     
#> 
#> other attached packages:
#> [1] dplyr_1.0.4
#> 
#> loaded via a namespace (and not attached):
#>  [1] knitr_1.31       magrittr_2.0.1   tidyselect_1.1.0 R6_2.5.0        
#>  [5] rlang_0.4.10     fansi_0.4.1      stringr_1.4.0    styler_1.3.2    
#>  [9] highr_0.8        tools_4.0.3      xfun_0.19        utf8_1.1.4      
#> [13] cli_2.3.0        DBI_1.1.0        htmltools_0.5.0  ellipsis_0.3.1  
#> [17] assertthat_0.2.1 yaml_2.2.1       digest_0.6.27    tibble_3.0.6    
#> [21] lifecycle_0.2.0  crayon_1.4.0     purrr_0.3.4      vctrs_0.3.6     
#> [25] fs_1.5.0         glue_1.4.2       evaluate_0.14    rmarkdown_2.6   
#> [29] reprex_1.0.0     stringi_1.5.3    compiler_4.0.3   pillar_1.4.7    
#> [33] generics_0.1.0   backports_1.2.1  renv_0.12.5      pkgconfig_2.0.3
@romainfrancois
Copy link
Member

@romainfrancois romainfrancois commented Feb 11, 2021

I believe this was accidentally fixed by #5750

library(dplyr, warn.conflicts = FALSE)
bad <- mtcars %>% tibble::as_tibble() %>% count(disp)
good <- mtcars %>% count(disp) %>% tibble::as_tibble()
waldo::compare(good, bad)
#> ✓ No differences

Created on 2021-02-11 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants