itsMeInMiami
itsMeInMiami

Reputation: 2763

Can reactable sort on rows?

I have a table in R that has the names of ML algorithms in the columns and features in the rows. The cells are holding the VIP variable importance rank if the algorithm used the feature. I would like to allow users to sort the table by clicking on a column heading or a row heading. Is it possible for reactable or a helper package like reactablefmtr or even some other package to do this? That is, can I allow a user to interactively sort a grid by either the rows or columns?

For example, in the table below, I would like to sort on feature "F" and have the data ordered on that rather than "Method A":

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)
library(reactable)

top10masked <- structure(
  list(
    thing = c(
      "Method A", "Method A", "Method A", 
      "Method A", "Method A", "Method A", "Method A", "Method A", "Method A", 
      "Method A", "Method B", "Method B", "Method B", "Method B", "Method B", 
      "Method B", "Method B", "Method B", "Method B", "Method B", "Method C", 
      "Method C", "Method C", "Method C", "Method C", "Method C", "Method C", 
      "Method C", "Method C", "Method C", "Method D", "Method D", "Method D", 
      "Method D", "Method D", "Method D", "Method D", "Method D", "Method D", 
      "Method D", "Method E", "Method E", "Method E", "Method E", "Method E", 
      "Method E", "Method E", "Method E", "Method E", "Method E", "Method F", 
      "Method F", "Method F", "Method F", "Method F", "Method F", "Method F", 
      "Method F", "Method F", "Method F", "Method G", "Method G", "Method G", 
      "Method G", "Method G", "Method G", "Method G", "Method G", "Method G", 
      "Method G", "Method H", "Method H", "Method H", "Method H", "Method H", 
      "Method H", "Method H", "Method H", "Method H", "Method H", "Method I", 
      "Method I", "Method I", "Method I", "Method I", "Method I", "Method I", 
      "Method I", "Method I", "Method I", "Method J", "Method J", "Method J", 
      "Method J", "Method J", "Method J", "Method J", "Method J", "Method J", 
      "Method J"), 
    variable = c(
      "Z", "W", "A", "N", "J", "AD", "AB", 
      "F", "G", "B", "Z", "R", "AC", "A", "J", "B", "D", "AB", "F", 
      "G", "Z", "A", "AE", "I", "Q", "AF", "AC", "E", "AG", "AH", "A", 
      "W", "Z", "F", "N", "V", "J", "AB", "AD", "X", "J", "F", "Z", 
      "AC", "E", "D", "X", "AD", "U", "AF", "Z", "AC", "F", "A", "V", 
      "B", "K", "AB", "R", "X", "Z", "R", "W", "T", "AC", "A", "F", 
      "V", "K", "AB", "AC", "Z", "W", "T", "A", "F", "V", "K", "AB", 
      "G", "AC", "Z", "J", "A", "G", "F", "V", "B", "H", "AB", "Z", 
      "N", "X", "V", "A", "F", "AB", "U", "AC", "W"), 
    Importance = c(
      1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 
      7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 
      2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 
      7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 
      2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 
      7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 
      2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)
  ), 
  row.names = c(NA, -100L), 
  class = "data.frame"
)

top10masked |>
  mutate(
    yes = case_when(
      Importance %in% c(1, 2, 3, 4, 5, 6, 7, 8, 9) ~ paste0("  ", Importance),
      Importance == 10 ~ paste0(" ", Importance),
      TRUE ~ NA_character_
    )
  ) |>
  # mutate(yes = as.character(Importance)) |>
  rename(Feature = variable) |>
  select(-Importance) |>
  pivot_wider(names_from = thing, values_from = yes) |>
  reactable(
    defaultPageSize = 30,
    defaultColDef = colDef(align = "center", sortNALast = TRUE),
    defaultSorted = "Method A",
    columns = list(
      Feature = colDef(align = "left") # overrides the default
    )
  )

Created on 2024-08-28 with reprex v2.1.1

Upvotes: 0

Views: 60

Answers (0)

Related Questions