基于外部列表的条件单元格高亮显示

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

Conditional cell highlighting based on external list

问题

以下是您要翻译的内容:

我想在数据表中突出显示满足以下条件的单元格,即其值包含在另一个列表中。

以下是我的可重现示例:

library(DT)
library(tidyverse)

df_table <- tibble(
  id = 1:10,
  V1 = rnorm(10)
)

my_id_list <- c(4, 6, 8)

datatable(df_table)

现在我想突出显示包含在 my_id_list 中的 id 值:
基于外部列表的条件单元格高亮显示

以下代码不起作用,但应该能说明我的意图:

datatable(df_table) %>%
  formatStyle("id", backgroundColor = if(id %in% my_id_list) {"yellow"})

我不确定是否可以通过R来实现解决方案。可能使用JavaScript实现是最合理的解决方案,如此问题中所示:
https://stackoverflow.com/questions/70955804/implementing-ifelse-or-if-else-in-datatable-output-to-conditionally-change-the

英文:

I want to highlight cells in a datatable on the condition that the values are contained in another list.

Here is my reproducible example:

library(DT)
library(tidyverse)

df_table &lt;- tibble(
  id = 1:10,
  V1 = rnorm(10)
)

my_id_list &lt;- c(4, 6, 8)

datatable(df_table)

Now I want to highlight the id values which are contained in the my_id_list:
基于外部列表的条件单元格高亮显示

The following code does not work, but should clarify my intention:

datatable(df_table) %&gt;%
  formatStyle(&quot;id&quot;, backgroundColor = if(id %in% my_id_list) {&quot;yellow&quot;})

I am unsure whether a solution can be achieved with the help of R. Probably a solution with javascript makes the most sense, as shown in this issue:
https://stackoverflow.com/questions/70955804/implementing-ifelse-or-if-else-in-datatable-output-to-conditionally-change-the

答案1

得分: 1

以下是翻译好的部分:

你可以创建一个带有二进制编码的额外列,然后使用 styleEqual 来匹配二进制列,但在实际的 DT 表中将其隐藏。

参考链接:https://rstudio.github.io/DT/010-style.html

library(DT)
library(dplyr)

datatable(data = df_table |> mutate(col = ifelse(id %in% my_id_list, 0, 1)),
          options = list(columnDefs = list(list(targets = 3, visible = FALSE)))) |> 
  formatStyle("id", "col", backgroundColor = styleEqual(c(0, 1), c("yellow", "")))

基于外部列表的条件单元格高亮显示

英文:

You can create an extra column with binary coding, then using styleEqual to match on the binary column, but hide it in the actual DT table.

Reference from https://rstudio.github.io/DT/010-style.html

library(DT)
library(dplyr)

datatable(data = df_table |&gt; mutate(col = ifelse(id %in% my_id_list, 0, 1)),
          options = list(columnDefs = list(list(targets = 3, visible = FALSE)))) |&gt; 
  formatStyle(&quot;id&quot;, &quot;col&quot;, backgroundColor = styleEqual(c(0, 1), c(&quot;yellow&quot;, &quot;&quot;)))

基于外部列表的条件单元格高亮显示

huangapple
  • 本文由 发表于 2023年5月25日 16:30:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330292.html
匿名

发表评论

匿名网友

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

确定