英文:
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 <- tibble(
id = 1:10,
V1 = rnorm(10)
)
my_id_list <- 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) %>%
formatStyle("id", backgroundColor = if(id %in% my_id_list) {"yellow"})
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 |> 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", "")))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论