单元格颜色按值和文本转换在gt中

huangapple go评论57阅读模式
英文:

cell colour by value and text transformations in gt

问题

我想利用 {gt} 的新的 data_color 重构,但我也想使用 text_transformation 并在表格中添加链接和自定义文本。例如,考虑以下数据:

library(dplyr)
library(gt)

dat <- data.frame(
  flag = c("safe", "out", "tie"),
  url = c("https://www.google.com", "https://www.yahoo.com", "https://www.bing.com"),
  value1 = c(1, NA, 3),
  value2 = c(1.1, NA, 8)
)

dat

    dat
  flag                    url value1 value2
1 safe https://www.google.com      1    1.1
2  out  https://www.yahoo.com     NA     NA
3  tie   https://www.bing.com      3    8.0

然后创建一个表格:

gt(dat) %>%
  cols_hide(columns = c("flag", "url")) %>%
  data_color(
    direction = "row",
    palette = "viridis"
  ) %>%
  text_transform(fn = function(x) glue("<a href=\"{dat$url}\" target=\"_blank\">{x}</a> {dat$flag}"))

这样,您可以创建一个具有活动链接的表格。

但是,如果有多列链接与相应的值列匹配,例如以下数据:

dat <- data.frame(
  flag = c("safe", "out", "tie"),
  url1 = c("https://www.google.com", "https://www.yahoo.com", "https://www.bing.com"),
  url2 = c("https://www.one.com", "https://www.two.com", "https://www.three.com"),
  value1 = c(1, NA, 3),
  value2 = c(1.1, NA, 8)
)

dat

您如何将url_1的值与value_1合并,并对 _2 执行相同操作?我知道可以使用 pivot_* 来获取正确的链接,但据我所知,这些列会变成字符串,然后我无法在 data_color 中使用它们。

有什么想法如何合并这两种功能?

英文:

I want to make use of {gt}'s nice new-ish data_color revamp but I would like to also make use of text_transformation and to add links and custom text to the table. So for example given this data:

library(dplyr)
library(gt)

dat &lt;- data.frame(
  flag = c(&quot;safe&quot;, &quot;out&quot;, &quot;tie&quot;),
  url = c(&quot;https://www.google.com&quot;, &quot;https://www.yahoo.com&quot;, &quot;https://www.bing.com&quot;),
  value1 = c(1, NA, 3),
  value2 = c(1.1, NA, 8)
)

dat

    dat
  flag                    url value1 value2
1 safe https://www.google.com      1    1.1
2  out  https://www.yahoo.com     NA     NA
3  tie   https://www.bing.com      3    8.0

and then create a table:

gt(dat) %&gt;%
  cols_hide(columns = c(&quot;flag&quot;, &quot;url&quot;)) %&gt;%
  data_color(
    direction = &quot;row&quot;,
    palette = &quot;viridis&quot;
  ) %&gt;%
  text_transform(fn = function(x) glue(&quot;&lt;a href=\&quot;{dat$url}\&quot; target=\&quot;_blank\&quot;&gt;{x}&lt;/a&gt; {dat$flag}&quot;))

So I can make a table that has active urls:

单元格颜色按值和文本转换在gt中

But what if I have multiple columns of urls that matches a corresponding vlaue column. So for example this data.frame:

dat &lt;- data.frame(
  flag = c(&quot;safe&quot;, &quot;out&quot;, &quot;tie&quot;),
  url1 = c(&quot;https://www.google.com&quot;, &quot;https://www.yahoo.com&quot;, &quot;https://www.bing.com&quot;),
  url2 = c(&quot;https://www.one.com&quot;, &quot;https://www.two.com&quot;, &quot;https://www.three.com&quot;),
  value1 = c(1, NA, 3),
  value2 = c(1.1, NA, 8)
)

dat

How can I add the values from url_1 to value_1 and the same for _2? I know I can pivot_* to get the right links but then AKAIK the columns become strings and then I can't use them in data_color.

Any ideas how I might merge these two abilities?

答案1

得分: 1

也许有一种更优雅的方法,但简单的方法是使用两个 text_transform,分别针对每对 url_value_ 列:

library(dplyr)
library(gt)
library(glue)

gt(dat) %>%
  cols_hide(columns = c("flag", "url1", "url2")) %>%
  data_color(
    direction = "row",
    palette = "viridis"
  ) %>%
  text_transform(
    fn = function(x) glue("<a href=\"{dat$url1}\" target=\"_blank\">{x}</a> {dat$flag}"),
    ocation = cells_body("value1")
  ) |>
  text_transform(
    fn = function(x) glue("<a href=\"{dat$url2}\" target=\"_blank\">{x}</a> {dat$flag}"),
    location = cells_body("value2")
  )

EDIT 另一种替代 for 循环的方法是使用 purrr::reduce,如下所示:

cols <- grep("value*", names(dat), value = TRUE)

gt(dat) %>%
  cols_hide(columns = c("flag", "url1", "url2")) %>%
  data_color(
    direction = "row",
    palette = "viridis"
  ) %>%
  purrr::reduce(cols, \(x, col) {
    url_col <- gsub("value", "url", col)
    text_transform(x,
      fn = function(x) glue("<a href=\"{dat[[url_col]]}\" target=\"_blank\">{x}</a> {dat$flag}"),
      location = cells_body(col)
    )
  }, .init = .)
英文:

Maybe there is a more elegant approach but a simple one would be to use two text_transform, one for each pair of url_ and value_ columns:

library(dplyr)
library(gt)
library(glue)

gt(dat) %&gt;%
  cols_hide(columns = c(&quot;flag&quot;, &quot;url1&quot;, &quot;url2&quot;)) %&gt;%
  data_color(
    direction = &quot;row&quot;,
    palette = &quot;viridis&quot;
  ) %&gt;%
  text_transform(
    fn = function(x) glue(&quot;&lt;a href=\&quot;{dat$url1}\&quot; target=\&quot;_blank\&quot;&gt;{x}&lt;/a&gt; {dat$flag}&quot;),
    ocation = cells_body(&quot;value1&quot;)
  ) |&gt;
  text_transform(
    fn = function(x) glue(&quot;&lt;a href=\&quot;{dat$url2}\&quot; target=\&quot;_blank\&quot;&gt;{x}&lt;/a&gt; {dat$flag}&quot;),
    location = cells_body(&quot;value2&quot;)
  )

EDIT And as an alternative approach to a for you could use purrr::reduce like so:

cols &lt;- grep(&quot;value*&quot;, names(dat), value = TRUE)

gt(dat) %&gt;%
  cols_hide(columns = c(&quot;flag&quot;, &quot;url1&quot;, &quot;url2&quot;)) %&gt;%
  data_color(
    direction = &quot;row&quot;,
    palette = &quot;viridis&quot;
  ) %&gt;%
  purrr::reduce(cols, \(x, col) {
    url_col &lt;- gsub(&quot;value&quot;, &quot;url&quot;, col)
    text_transform(x,
      fn = function(x) glue(&quot;&lt;a href=\&quot;{dat[[url_col]]}\&quot; target=\&quot;_blank\&quot;&gt;{x}&lt;/a&gt; {dat$flag}&quot;),
      location = cells_body(col)
    )
  }, .init = .)

答案2

得分: 1

@stefan:这段代码太长了,无法作为评论添加,但是采用你的方法并进行泛化,这里可以使用:

dat <- tibble(
  flag = c("safe", "out", "tie"),
  url1 = c("https://www.google.com", "https://www.yahoo.com", "https://www.bing.com"),
  url2 = c("https://www.one.com", "https://www.two.com", "https://www.three.com"),
  value1 = c(1, NA, 3),
  value2 = c(1.1, NA, 8)
)

gt_table <- gt(dat) %>%
  cols_hide(columns = c("flag", "url1", "url2")) %>%
  data_color(
    direction = "row",
    palette = "viridis"
  )


cols <- grep("value*", names(dat), value = TRUE)

for (i in seq_along(cols)) {
  url_col <- gsub("value", "url", cols[i])
  gt_table <- gt_table %>%
    text_transform(
      fn = function(x) glue("<a href=\"{dat[,url_col, drop = TRUE]}\" target=\"_blank\">{x}</a>"),
      location = cells_body(cols[i])
    )
}

gt_table

这是翻译好的代码部分。

英文:

@stefan: This was too long to add as a comment but taking your approach and generalizing it, this could work here:

dat &lt;- tibble(
  flag = c(&quot;safe&quot;, &quot;out&quot;, &quot;tie&quot;),
  url1 = c(&quot;https://www.google.com&quot;, &quot;https://www.yahoo.com&quot;, &quot;https://www.bing.com&quot;),
  url2 = c(&quot;https://www.one.com&quot;, &quot;https://www.two.com&quot;, &quot;https://www.three.com&quot;),
  value1 = c(1, NA, 3),
  value2 = c(1.1, NA, 8)
)

gt_table &lt;- gt(dat) %&gt;%
  cols_hide(columns = c(&quot;flag&quot;, &quot;url1&quot;, &quot;url2&quot;)) %&gt;%
  data_color(
    direction = &quot;row&quot;,
    palette = &quot;viridis&quot;
  )


cols &lt;- grep(&quot;value*&quot;, names(dat), value = TRUE)

for (i in seq_along(cols)) {
  url_col &lt;- gsub(&quot;value&quot;, &quot;url&quot;, cols[i])
  gt_table &lt;- gt_table %&gt;%
    text_transform(
      fn = function(x) glue(&quot;&lt;a href=\&quot;{dat[,url_col, drop = TRUE]}\&quot; target=\&quot;_blank\&quot;&gt;{x}&lt;/a&gt;&quot;),
      location = cells_body(cols[i])
    )
}

gt_table

huangapple
  • 本文由 发表于 2023年6月13日 05:07:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定