我希望将reactable的groupBy聚合与显示来自其他单元格的信息的能力结合起来。例如,我可以显示/组合来自其他列的信息,如:
library(reactable)
library(dplyr)
library(htmltools)
data <- starwars %>%
select(character = name, height, mass, gender, homeworld, species)
reactable(
data,
columns = list(
character = colDef(
# Show species under character names
cell = function(value, index) {
species <- data$species[index]
species <- if (!is.na(species)) species else "Unknown"
div(
div(style = "font-weight: 600", value),
div(style = "font-size: 0.75rem", species)
)
}
),
species = colDef(show = FALSE)
),
defaultPageSize = 6
)

独立地,我可以使用groupBy参数聚合行:
reactable(
data,
groupBy = c("character"),
defaultPageSize = 6
)

但当试图将两者结合起来时,事情并不像预期的那样起作用:
reactable(
data,
groupBy = c("character"),
columns = list(
character = colDef(
# Show species under character names
cell = function(value, index) {
species <- data$species[index]
species <- if (!is.na(species)) species else "Unknown"
div(
div(style = "font-weight: 600", value),
div(style = "font-size: 0.75rem", species)
)
}
),
species = colDef(show = FALSE)
),
defaultPageSize = 6
)

在grouped函数中有一个colDef()参数,我认为它有一个答案,但我一直无法让它工作起来。
以下是一些可能有帮助的链接:
发布于 2022-11-16 19:13:04
最后,我使用了以下方法:创建一个新列,将HTML格式放入列中,然后在colDef中使用colDef
library(dplyr)
library(reactable)
data <- starwars %>%
select(character = name, height, mass, gender, homeworld, species)
data %>%
mutate(
character_and_species = glue::glue("<strong>{character}</strong><br><small>{species}</small>")
) %>%
reactable(
groupBy = c("character_and_species"),
columns = list(
character_and_species = colDef(html = TRUE),
species = colDef(show = FALSE)
),
defaultPageSize = 6
)在以下方面的成果:

仍然对其他方法感兴趣,但这实现了预期的结果。
https://stackoverflow.com/questions/74337314
复制相似问题