Order rows in reactablefmtr non alphabetically

62 Views Asked by At

I'm using reactable to create a table but I want my rows to be in order of the dataframe as read in, not alphabetically. Please advise.

reactable(data)

data <- data.frame (Indicator  = c("Water Quality", "Benthic Macroinvertebrates","Vegetation"),
                  Value1 = c(40, 50, 10),
                  Value2 = c(20, 30, 10),
                  Value3 = c(10, 40, 30))


data %>%
  reactable(.,
            theme = nytimes(centered = TRUE, header_font_size = 11),
            defaultSorted = "Indicator",
            pagination = FALSE,
            rowStyle = group_border_sort("Indicator"),
            columns = list(
              Indicator = colDef(
                name = "Indicator",
                style = group_merge_sort("Indicator")),
              Value1 = colDef(
                cell = data_bars(
                  data = data,
                  text_position = "outside-base", 
                  max_value=100,
                  fill_color ='#4f94CD')),
              Value2 = colDef(
                cell = data_bars(
                  data = data,
                  text_position = "outside-base", 
                  max_value=100,
                  fill_color ='#EE5C42')),
             Value3 = colDef(
                cell = data_bars(
                  data = data, 
                  max_value=100,
                  # bar_height = 6,
                  text_position = "outside-base", 
                  fill_color = '#FFEC8B'))
              
            ))

When I print, the table comes out in alphabetical order rather than the order the dataframe is read in.

reordered to alphabetical

1

There are 1 best solutions below

0
On

As you have set defaultSorted = "Indicator" the table will be sorted alphabetically by Indicator. Hence, to preserve the order of your data one option would be to drop that argument:

library(reactable)
library(reactablefmtr)

data %>%
  reactable(.,
    theme = nytimes(centered = TRUE, header_font_size = 11),
    pagination = FALSE,
    rowStyle = group_border_sort("Indicator"),
    columns = list(
      Indicator = colDef(
        name = "Indicator",
        style = group_merge_sort("Indicator")
      ),
      Value1 = colDef(
        cell = data_bars(
          data = data,
          text_position = "outside-base",
          max_value = 100,
          fill_color = "#4f94CD"
        )
      ),
      Value2 = colDef(
        cell = data_bars(
          data = data,
          text_position = "outside-base",
          max_value = 100,
          fill_color = "#EE5C42"
        )
      ),
      Value3 = colDef(
        cell = data_bars(
          data = data,
          max_value = 100,
          # bar_height = 6,
          text_position = "outside-base",
          fill_color = "#FFEC8B"
        )
      )
    )
  )

enter image description here