I would like to make some rows expand on click, i.e. by clicking on the arrow. Is there a way in reactable
to add java script code to specific rows? There exists and option for expanding/collapsing all rows on click in reactable
(see here), but it does not do exactly what I need. Is there any reactable/javascript guru out there who has an idea how this might be feasible.
The required output is designed with kableExtra
but I think my problem is more suited for reactable
.
df_open <- tibble::tibble(
Category = c("Fruits ▼︎", "Apple", "Banana",
"Kiwi", "Vegetable ▼︎", "Carrots ▼︎", "Red Carrots", "Orange Carrots", "Diary"),
Value_sum = c(1:9),
Value_one = c(10:18),
Value_two = c(200:208)
)
df_closed <- tibble::tibble(
Category = c("Fruits ▶︎","Vegetable ▶︎", "Diary"),
Value_sum = c(1, 5,9),
Value_one = c(10,14,18),
Value_two = c(200, 204,208)
)
library(kableExtra)
# all collapsed, everything shown
kbl(df_open, escape = FALSE) |>
kable_styling(bootstrap_options = "bordered") |>
row_spec(row = c(2:4, 6), extra_css = "padding-left:30px;") |>
row_spec(row = c(7,8), extra_css = "padding-left:50px")
# all closed
kbl(df_closed, escape = FALSE) |>
kable_styling(bootstrap_options = "bordered")
Completely folded table:
Raw Data:
df <- tibble::tibble(
Category = c("Fruits", "Apple", "Banana", "Kiwi", "Vegetable", "Carrots", "Red Carrots", "Orange Carrots", "Diary"),
Value_sum = c(1:9),
Value_one = c(10:18),
Value_two = c(200:208),
Another_column = "ABC"
)
My attempt using this blog post
library(dplyr)
library(reactable)
top_level <- df |>
mutate(id = c(1,1,1,1,2,2, 2,2,3),
id_large = c(1, NA, NA, NA,2, NA, NA, NA, 3)) |>
filter(!is.na(id_large)) |>
relocate(id, .before = 1) |>
select(-id_large)
second_level <- df |>
mutate(id = c(1,1,1,1,2,2, 2,2,3),
id_large = c(1, NA, NA, NA,2, NA, NA, NA, 3)) |>
filter(is.na(id_large)) |>
relocate(id, .before = 1) |>
select(-id_large)
reactable(
data = top_level,
compact = TRUE,
striped = TRUE,
resizable = TRUE,
columns = list(
id = colDef(name = "ID", show = FALSE)),
details = function(index) { # index is the row number of current row.
sec_lvl = second_level[second_level$id == top_level$id[index], ]
reactable(data = sec_lvl,
compact = TRUE,
bordered = TRUE,
resizable = TRUE,
colna
columns = list(
id = colDef(name = "ID", show = FALSE))
)
}
)