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

Bug: additional column "stringsAsFactors" shows up when using dplyr::group_by() and rownames=TRUE #283

Closed
daattali opened this issue Nov 3, 2022 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@daattali
Copy link

daattali commented Nov 3, 2022

This bug happens whenever you use a dplyr::group_by() with more than one grouping variable and also use reactable(rownames = TRUE)

library(dplyr)
reactable::reactable(
    data.frame(a = 1:2, b = 1:2) %>% group_by(a, b) %>% summarise(n = n()),
    rownames = TRUE
)

image

If you set rownames = FALSE or you group by only one variable, this doesn't happen

@glin glin added the bug Something isn't working label Nov 5, 2022
@glin glin added this to the v0.4.0 milestone Nov 5, 2022
@glin
Copy link
Owner

glin commented Nov 5, 2022

Thanks, this is now fixed in 9a5ff19:

  • Using reactable() on a dplyr grouped data frame ( dplyr::group_by() or grouped_df) with rownames = TRUE no longer adds a stringsAsFactors column to the table (@daattali, #283).

This was a really weird issue. For some reason, cbind(..., stringsAsFactors = FALSE) works on data.frames, but not dplyr's grouped_df. Depending on how you interpret cbind(), I guess it could be more of a bug with dplyr's dplyr:::cbind.grouped_df() method. ?cbind says..

... For the "data.frame" method of cbind these can be further arguments to data.frame such as stringsAsFactors.

Here's a minimal repro of cbind()ing a data frame and then a grouped_df:

cbind(x = "a", data.frame(y = 2), stringsAsFactors = FALSE)
#   x y
# 1 a 2

cbind(x = "a", dplyr::grouped_df(data.frame(y = 2), "y"), stringsAsFactors = FALSE)
# # A tibble: 1 x 3
#   x         y stringsAsFactors
#   <chr> <dbl> <lgl>           
# 1 a         2 FALSE     

If you think so, you can report it on dplyr's issue tracker as well, but I imagine very few users are going to be using stringsAsFactors with tibbles/grouped_df anyway.

For a workaround, you can ungroup the data frame to turn it back into a regular tibble:

library(dplyr)
reactable::reactable(
    data.frame(a = 1:2, b = 1:2) %>% group_by(a, b) %>% summarise(n = n()) %>% ungroup(),
    rownames = TRUE
)

@daattali
Copy link
Author

daattali commented Nov 5, 2022

Thanks for the quick fix. Reported in dplyr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants