glin / reactable

Interactive data tables for R
https://glin.github.io/reactable
Other
612 stars 79 forks source link

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

Closed daattali closed 1 year ago

daattali commented 1 year ago

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 commented 1 year ago

Thanks, this is now fixed in https://github.com/glin/reactable/commit/9a5ff19d4d313f4a85b23bc7a14f7757588aecd5:

  • 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 commented 1 year ago

Thanks for the quick fix. Reported in dplyr