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

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

cell colour by value and text transformations in gt

问题

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

  1. library(dplyr)
  2. library(gt)
  3. dat <- data.frame(
  4. flag = c("safe", "out", "tie"),
  5. url = c("https://www.google.com", "https://www.yahoo.com", "https://www.bing.com"),
  6. value1 = c(1, NA, 3),
  7. value2 = c(1.1, NA, 8)
  8. )
  9. dat
  10. dat
  11. flag url value1 value2
  12. 1 safe https://www.google.com 1 1.1
  13. 2 out https://www.yahoo.com NA NA
  14. 3 tie https://www.bing.com 3 8.0

然后创建一个表格:

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

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

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

  1. dat <- data.frame(
  2. flag = c("safe", "out", "tie"),
  3. url1 = c("https://www.google.com", "https://www.yahoo.com", "https://www.bing.com"),
  4. url2 = c("https://www.one.com", "https://www.two.com", "https://www.three.com"),
  5. value1 = c(1, NA, 3),
  6. value2 = c(1.1, NA, 8)
  7. )
  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:

  1. library(dplyr)
  2. library(gt)
  3. dat &lt;- data.frame(
  4. flag = c(&quot;safe&quot;, &quot;out&quot;, &quot;tie&quot;),
  5. url = c(&quot;https://www.google.com&quot;, &quot;https://www.yahoo.com&quot;, &quot;https://www.bing.com&quot;),
  6. value1 = c(1, NA, 3),
  7. value2 = c(1.1, NA, 8)
  8. )
  9. dat
  10. dat
  11. flag url value1 value2
  12. 1 safe https://www.google.com 1 1.1
  13. 2 out https://www.yahoo.com NA NA
  14. 3 tie https://www.bing.com 3 8.0

and then create a table:

  1. gt(dat) %&gt;%
  2. cols_hide(columns = c(&quot;flag&quot;, &quot;url&quot;)) %&gt;%
  3. data_color(
  4. direction = &quot;row&quot;,
  5. palette = &quot;viridis&quot;
  6. ) %&gt;%
  7. 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:

  1. dat &lt;- data.frame(
  2. flag = c(&quot;safe&quot;, &quot;out&quot;, &quot;tie&quot;),
  3. url1 = c(&quot;https://www.google.com&quot;, &quot;https://www.yahoo.com&quot;, &quot;https://www.bing.com&quot;),
  4. url2 = c(&quot;https://www.one.com&quot;, &quot;https://www.two.com&quot;, &quot;https://www.three.com&quot;),
  5. value1 = c(1, NA, 3),
  6. value2 = c(1.1, NA, 8)
  7. )
  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_ 列:

  1. library(dplyr)
  2. library(gt)
  3. library(glue)
  4. gt(dat) %>%
  5. cols_hide(columns = c("flag", "url1", "url2")) %>%
  6. data_color(
  7. direction = "row",
  8. palette = "viridis"
  9. ) %>%
  10. text_transform(
  11. fn = function(x) glue("<a href=\"{dat$url1}\" target=\"_blank\">{x}</a> {dat$flag}"),
  12. ocation = cells_body("value1")
  13. ) |>
  14. text_transform(
  15. fn = function(x) glue("<a href=\"{dat$url2}\" target=\"_blank\">{x}</a> {dat$flag}"),
  16. location = cells_body("value2")
  17. )

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

  1. cols <- grep("value*", names(dat), value = TRUE)
  2. gt(dat) %>%
  3. cols_hide(columns = c("flag", "url1", "url2")) %>%
  4. data_color(
  5. direction = "row",
  6. palette = "viridis"
  7. ) %>%
  8. purrr::reduce(cols, \(x, col) {
  9. url_col <- gsub("value", "url", col)
  10. text_transform(x,
  11. fn = function(x) glue("<a href=\"{dat[[url_col]]}\" target=\"_blank\">{x}</a> {dat$flag}"),
  12. location = cells_body(col)
  13. )
  14. }, .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:

  1. library(dplyr)
  2. library(gt)
  3. library(glue)
  4. gt(dat) %&gt;%
  5. cols_hide(columns = c(&quot;flag&quot;, &quot;url1&quot;, &quot;url2&quot;)) %&gt;%
  6. data_color(
  7. direction = &quot;row&quot;,
  8. palette = &quot;viridis&quot;
  9. ) %&gt;%
  10. text_transform(
  11. fn = function(x) glue(&quot;&lt;a href=\&quot;{dat$url1}\&quot; target=\&quot;_blank\&quot;&gt;{x}&lt;/a&gt; {dat$flag}&quot;),
  12. ocation = cells_body(&quot;value1&quot;)
  13. ) |&gt;
  14. text_transform(
  15. fn = function(x) glue(&quot;&lt;a href=\&quot;{dat$url2}\&quot; target=\&quot;_blank\&quot;&gt;{x}&lt;/a&gt; {dat$flag}&quot;),
  16. location = cells_body(&quot;value2&quot;)
  17. )

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

  1. cols &lt;- grep(&quot;value*&quot;, names(dat), value = TRUE)
  2. gt(dat) %&gt;%
  3. cols_hide(columns = c(&quot;flag&quot;, &quot;url1&quot;, &quot;url2&quot;)) %&gt;%
  4. data_color(
  5. direction = &quot;row&quot;,
  6. palette = &quot;viridis&quot;
  7. ) %&gt;%
  8. purrr::reduce(cols, \(x, col) {
  9. url_col &lt;- gsub(&quot;value&quot;, &quot;url&quot;, col)
  10. text_transform(x,
  11. 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;),
  12. location = cells_body(col)
  13. )
  14. }, .init = .)

答案2

得分: 1

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

  1. dat <- tibble(
  2. flag = c("safe", "out", "tie"),
  3. url1 = c("https://www.google.com", "https://www.yahoo.com", "https://www.bing.com"),
  4. url2 = c("https://www.one.com", "https://www.two.com", "https://www.three.com"),
  5. value1 = c(1, NA, 3),
  6. value2 = c(1.1, NA, 8)
  7. )
  8. gt_table <- gt(dat) %>%
  9. cols_hide(columns = c("flag", "url1", "url2")) %>%
  10. data_color(
  11. direction = "row",
  12. palette = "viridis"
  13. )
  14. cols <- grep("value*", names(dat), value = TRUE)
  15. for (i in seq_along(cols)) {
  16. url_col <- gsub("value", "url", cols[i])
  17. gt_table <- gt_table %>%
  18. text_transform(
  19. fn = function(x) glue("<a href=\"{dat[,url_col, drop = TRUE]}\" target=\"_blank\">{x}</a>"),
  20. location = cells_body(cols[i])
  21. )
  22. }
  23. gt_table

这是翻译好的代码部分。

英文:

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

  1. dat &lt;- tibble(
  2. flag = c(&quot;safe&quot;, &quot;out&quot;, &quot;tie&quot;),
  3. url1 = c(&quot;https://www.google.com&quot;, &quot;https://www.yahoo.com&quot;, &quot;https://www.bing.com&quot;),
  4. url2 = c(&quot;https://www.one.com&quot;, &quot;https://www.two.com&quot;, &quot;https://www.three.com&quot;),
  5. value1 = c(1, NA, 3),
  6. value2 = c(1.1, NA, 8)
  7. )
  8. gt_table &lt;- gt(dat) %&gt;%
  9. cols_hide(columns = c(&quot;flag&quot;, &quot;url1&quot;, &quot;url2&quot;)) %&gt;%
  10. data_color(
  11. direction = &quot;row&quot;,
  12. palette = &quot;viridis&quot;
  13. )
  14. cols &lt;- grep(&quot;value*&quot;, names(dat), value = TRUE)
  15. for (i in seq_along(cols)) {
  16. url_col &lt;- gsub(&quot;value&quot;, &quot;url&quot;, cols[i])
  17. gt_table &lt;- gt_table %&gt;%
  18. text_transform(
  19. 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;),
  20. location = cells_body(cols[i])
  21. )
  22. }
  23. 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:

确定