glin / reactable

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

Sort factors by levels #202

Open ccamara opened 3 years ago

ccamara commented 3 years ago

Reactable is configured to let columns to be sortable, which is great. Unfortunately, the sorting doesn't seem to work with factor levels.

While by default factor levels are sorted alphabetically, this is not always the case.

Take the code below as an example: I would love to see the column species sorted according to the levels' order (Human, and then the rest of levels, alphabetically), but instead, they are sorted alphabetically.

library(tidyverse)

starwars2<- starwars %>% 
  mutate(species = fct_relevel(as.factor(species), "Human"))

reactable(
  starwars2,
  defaultSortOrder = "desc",
  columns = list(
    species = colDef(defaultSortOrder = "asc")
  )
)

This is the output:

> levels(starwars2$species)
 [1] "Human"          "Aleena"         "Besalisk"       "Cerean"         "Chagrian"       "Clawdite"      
 [7] "Droid"          "Dug"            "Ewok"           "Geonosian"      "Gungan"         "Hutt"          
[13] "Iktotchi"       "Kaleesh"        "Kaminoan"       "Kel Dor"        "Mirialan"       "Mon Calamari"  
[19] "Muun"           "Nautolan"       "Neimodian"      "Pau'an"         "Quermian"       "Rodian"        
[25] "Skakoan"        "Sullustan"      "Tholothian"     "Togruta"        "Toong"          "Toydarian"     
[31] "Trandoshan"     "Twi'lek"        "Vulptereen"     "Wookiee"        "Xexto"          "Yoda's species"
[37] "Zabrak" 

reactable

mrworthington commented 3 weeks ago

Curious if anything ever came of this?