将text_transform()应用于R GT中的行组
在{gt}软件包中,我想在“行组”标题上使用text_transform()以渲染html,但是我得到了“ resolve_location”错误的“无适用方法”。
在下面的示例中,您可以看到text_transform()如果位置参数为cell_body()
(这不是我实际想要的),但如果它是cells_row_groups()这是我想要的。
想法?
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>")
}
)
In the {gt} package I want to use text_transform() on the row group titles in order to render the HTML but I'm getting the `no applicable method for 'resolve_location' error.
In my example below, you can see that text_transform() works if the locations argument is cells_body()
(which is not what I actually want) but not if it's cells_row_groups()
which is what I want.
Thoughts?
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>")
}
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一些反复试验和查看渲染的html代码,我使用
gt :: html
:After some trial and error and a look at the rendered html code I figured out a solution using
gt::html
: