glin / reactable

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

Can details take both row and column argument ? #376

Open statquant opened 3 weeks ago

statquant commented 3 weeks ago

Hello, looking at the doumentation I see in ?reactable

details: Additional content to display when expanding a row. An R function that takes the row index and column name as arguments, or a ‘JS()’ function that takes a row info object as an argument. Can also be a ‘colDef()’ to customize the details expander column.

However when I try to pass 2 arguments the second argument is worth ".details", is that expected ?

data <- unique(CO2[, c("Plant", "Type")])
reactable(data, details = function(index, column) {
  browser()
  plant_data <- CO2[CO2$Plant == data$Plant[index], ]
  htmltools::div(style = "padding: 1rem",
    reactable(plant_data, outlined = TRUE)
  )
})

What I am hoping to achieve is a table where each cell would contain a table itelf. FYI each cell within a column would contain a table with a fixed set of columns but different number of rows.

glin commented 6 hours ago

Yes, the second argument of the R function is the column name. For the default details column, that's actually a built-in column named .details which you can ignore. If you want to add row details to every cell, you can use the default column definition:

reactable(
  MASS::Cars93,
  defaultColDef = colDef(
    details = function(index, column) {
      paste(index, column)
    }
  )
)

Then use the row index and column name to determine what to render within that cell's row details.