I have this dataset in R. I'd like to transform it with R package reacatable.
dt <-
data.table(
month = seq(1, 12),
aa_2019 = seq(1, 12),
bb_2019 = seq(1, 12),
aa_2020 = seq(1, 12),
bb_2020 = seq(1, 12),
aa_2021 = seq(1, 12),
bb_2021 = seq(1, 12),
aa_2022 = seq(1, 12),
bb_2022 = seq(1, 12)
)
Desired output looks like this. Is there a way how to make it automatically? Imagine having sequence of years from 1900 to 2023.
reactable(
dt,
columns = list(
aa_2019 = colDef(name = "aa"),
bb_2019 = colDef(name = "bb"),
aa_2020 = colDef(name = "aa"),
bb_2020 = colDef(name = "bb"),
aa_2021 = colDef(name = "aa"),
bb_2021 = colDef(name = "bb"),
aa_2022 = colDef(name = "aa"),
bb_2022 = colDef(name = "bb")
),
columnGroups = list(
colGroup(name = "2019", columns = c("aa_2019", "bb_2019")),
colGroup(name = "2020", columns = c("aa_2020", "bb_2020")),
colGroup(name = "2021", columns = c("aa_2021", "bb_2021")),
colGroup(name = "2022", columns = c("aa_2022", "bb_2022"))
)
)
Here is one approach to create your
colDef
s andcolGroup
s dynamically: