adajam
adajam

Reputation: 221

Using subsetted data in Reactable table

In reactable, you can make edits to named columns as follows:

    columns = list(
        Species = colDef(minWidth = 140),
        AnotherNamedColumn = colDef(align = "center")
)

The data I am using changes weekly, and so I can't use a named column. I need to subset from a df this:

    columns = list(
        df[1,2] = colDef(minWidth = 140),
        df[1,3] = colDef(align = "center")
)

But reactable doesn't like this. Does anyone know how I can get this to work and actually get reactable to evaluate the subset code so I can 'point' to the correct column without naming it?

Upvotes: 0

Views: 501

Answers (1)

stefan
stefan

Reputation: 124223

If I got you right then one option to achieve your desired result would look like so:

  1. Put your colDefs in a list
  2. Set the names of the list according to column positions in your df
  3. Pass the named list to the columns argument
library(reactable)

df <- iris
columns <- list(colDef(minWidth = 140), colDef(align = "center"))
columns <- setNames(columns, names(df)[2:3])
reactable(df, columns = columns)

Upvotes: 1

Related Questions