在{gt}包中,我想对行组标题使用text\u transform(),以便呈现HTML,但我得到了"没有适用于"resolve\u location"的方法"错误.
在下面的示例中,您可以看到,如果locations参数为cells_body()
(这不是我真正想要的),text\u transform()可以工作,但如果我想要的是cells_row_groups()
,则text\u transform()不能工作.
思想?
Zev公司
# As an experiment, I put HTML in both a value and in the groups, though
# in the real data there is only HTML in groups.
tbl <- tibble(values = c("test<sup>2</sup>", 2:4), groups = c("x<sup>2</sup>", "x<sup>2</sup>", "y", "y"))
unescape_html <- function(str){
xml2::xml_text(xml2::read_html(paste0("<x>", str, "</x>")))
}
# Error, no applicable method for resolve_location
tbl |>
gt::gt(groupname_col = "b") |>
gt::text_transform(
locations = gt::cells_row_groups(),
fn = function(x){
x <- purrr::map_chr(x, unescape_html)
paste("<span style=color:red;>", x, "</span>")
}
)
# This works so it shows that I'm close :)
tbl |>
gt::gt(groupname_col = "b") |>
gt::text_transform(
locations = gt::cells_body(columns = 1),
fn = function(x){
x <- purrr::map_chr(x, unescape_html)
paste("<span style=color:red;>", x, "</span>")
}
)