使用 if else 对单元格进行条件格式化?或者在 R 中隐藏 DT 包中的列

conditional formatting of cells using if else? or hiding columns in DT package in R

我有一个 table 看起来像这样。

data_wide <-data.frame(id=c(1,2,3,4),book1=c("","","","age"),book2=c("covid","alive","",""),book3=c("","income","","location"))

我正在尝试使用 R 中的 DT 包在 html 中呈现此 table,以便所有值都被隐藏并且可以通过鼠标悬停来查看。为了进一步澄清,我的目标是根据值使单元格的背景不同,例如,没有值的单元格可以是浅蓝色,而有值的单元格可以是绿色,当我将鼠标悬停在绿色单元格上时,我能够查看此值。以下是我为实现目标而进行的非常天真的尝试:

library(DT)
library(stringr)

data_wide <- data_wide %>% 
  mutate(b1 = as.numeric(str_detect(string = book1, pattern = ""))) %>% 
  mutate(b2 = as.numeric(str_detect(string = book2, pattern = ""))) %>% 
  mutate(b3 = as.numeric(str_detect(string = book3, pattern = ""))) 


datatable(data_wide, options = list(columnDefs = list(list(
  targets = 2:4,
  render = JS(
    "function(data, type, row, meta) {",
    "return type === 'display' && data.length > 6?",
    "'<span title=\"' + data + '\">' + data.substr(0, 20) + '...</span>' : data;",
    "}")
))), callback = JS('table.page(3).draw(false);'))%>%
formatStyle(
'book1',"b1",
color = "lightgreen",
border = '2px solid #FFFFFF',
backgroundColor = styleEqual(c(0, 1), c('lightblue', 'lightgreen'))) %>%
formatStyle(
'book2',"b2",
border = '2px solid #FFFFFF',
color = "lightgreen",
backgroundColor = styleEqual(c(0, 1), c('lightblue', 'lightgreen'))) %>%
formatStyle(
'book3',"b3",
border = '2px solid #FFFFFF',
color = "lightgreen",
backgroundColor = styleEqual(c(0, 1), c('lightblue', 'lightgreen'))) 

为了便于说明,我添加了 b1、b2、b3 列,以便我可以从中借用值来对 book1、book2 和 book3 的单元格进行颜色编码。但是我无法删除它们 (b1,b2,b3),因为不再需要它们。我尝试在选项中添加 list(visible = FALSE, target = c(5,6,7) 但这似乎不起作用,也许是因为该选项已用于启用鼠标悬停?我已经研究了条件格式在这个论坛和其他资源上很好,但看起来只适用于整数 (0,1..etc),我在这个 table 中有字符串。我很茫然,希望找到解决方案。

非常感谢对此的任何帮助,并乐于接受其他想法。

谢谢。

你可以试试这个吗:

data_wide <-data.frame(id=c(1,2,3,4),book1=c("","","","age"),book2=c("covid","alive","",""),book3=c("","income","","location"))

library(DT)
library(stringr)
library(dplyr)

data_wide <- data_wide %>% 
  mutate(b1 = as.numeric(str_detect(string = book1, pattern = ""))) %>% 
  mutate(b2 = as.numeric(str_detect(string = book2, pattern = ""))) %>% 
  mutate(b3 = as.numeric(str_detect(string = book3, pattern = ""))) 


datatable(data_wide, options = list(columnDefs = list(list(
  visible=FALSE, targets=c(5,6,7),
  render = JS(
    "function(data, type, row, meta) {",
    "return type === 'display' && data.length > 6?",
    "'<span title=\"' + data + '\">' + data.substr(0, 20) + '...</span>' : data;",
    "}")
))), callback = JS('table.page(3).draw(false);'))%>%
  formatStyle(
    'book1',"b1",
    color = "lightgreen",
    border = '2px solid #FFFFFF',
    backgroundColor = styleEqual(c(0, 1), c('lightblue', 'lightgreen'))) %>%
  formatStyle(
    'book2',"b2",
    border = '2px solid #FFFFFF',
    color = "lightgreen",
    backgroundColor = styleEqual(c(0, 1), c('lightblue', 'lightgreen'))) %>%
  formatStyle(
    'book3',"b3",
    border = '2px solid #FFFFFF',
    color = "lightgreen",
    backgroundColor = styleEqual(c(0, 1), c('lightblue', 'lightgreen')) )
        
                

                                                             

好的,所以我将在这里回答我自己的问题,代码需要使用“visible = FALSE”的列表中的另一个列表,正如@LocoGris 指出的那样。没有在列表中添加列表,省略号被破坏了。

datatable(
  data_wide, 
  plugins = "ellipsis",
  options = list(
    columnDefs = list(list(
      targets = c(2,3,4),
      render = JS("$.fn.dataTable.render.ellipsis( 17, false )")
    ),list(visible=FALSE,targets = c(5,6,7)))
  )
)%>%
  formatStyle(
    'book1',"b1",
    color = "lightgreen",
    border = '2px solid #FFFFFF',
    backgroundColor = styleEqual(c(0, 1), c('lightblue', 'lightgreen'))) %>%
  formatStyle(
    'book2',"b2",
    border = '2px solid #FFFFFF',
    color = "lightgreen",
    backgroundColor = styleEqual(c(0, 1), c('lightblue', 'lightgreen'))) %>%
  formatStyle(
    'book3',"b3",
    border = '2px solid #FFFFFF',
    color = "lightgreen",
    backgroundColor = styleEqual(c(0, 1), c('lightblue', 'lightgreen')) )