将text_transform()应用于R GT中的行组

发布于 2025-02-10 04:42:09 字数 1462 浏览 1 评论 0原文

在{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>")
    }
  )

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箜明 2025-02-17 04:42:09

经过一些反复试验和查看渲染的html代码,我使用gt :: html

tbl <- tibble::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>")))
}

tbl |> 
  gt::gt(groupname_col = "groups") |> 
  gt::text_transform(
    locations = gt::cells_row_groups(),
    fn = function(x) {
      purrr::map(x, ~ gt::html(paste("<span style=color:blue;>", .x, "</span>")))
    }
  ) |> 
  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>")
    }
  )

“

After some trial and error and a look at the rendered html code I figured out a solution using gt::html:

tbl <- tibble::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>")))
}

tbl |> 
  gt::gt(groupname_col = "groups") |> 
  gt::text_transform(
    locations = gt::cells_row_groups(),
    fn = function(x) {
      purrr::map(x, ~ gt::html(paste("<span style=color:blue;>", .x, "</span>")))
    }
  ) |> 
  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>")
    }
  )

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文