如何为R中的数据框/表中的特定单元着色?

gbr*_*017 9 css formatting r

我想在以下数据框中为特定单元格着色.例如,在inputval列中,我想要突出显示[0.8, 0.9)洋红色范围内的单元格,以及[0.7, 0.8)蓝色范围内同一列中的单元格.同样,我希望值为1的输出列单元格为洋红色,而值为4的输出列单元格为蓝色.对于数据框中的其余单元格,我希望它们保持白色.

我有以下可重现的代码,仅按行突出显示,并且仅限于使用洋红色和白色进行着色.如何添加其他颜色并按单元格添加?

set.seed(123)
df <- data.frame(id       = sample(1:100, 20, replace = TRUE),
                 inputval = sample(seq(0, 1, by=0.01), 20, replace = TRUE),
                 outcome  = sample(1:4, 20, replace = TRUE))

cols <- with(df, ifelse(outcome == 1, 'magenta', 'white'))

library('htmlTable')
htmlTable(as.matrix(df), col.rgroup = cols)
Run Code Online (Sandbox Code Playgroud)

我意识到添加不同颜色的问题在于ifelse调用,with这限制了我只有洋红色和白色.我怎样才能在这里添加另一个条件?

虽然我知道是什么导致了多种颜色问题,但我对于如何仅为特定细胞着色非常无能为力.

这是与此问题的接受答案相同的示例.谢谢!

luk*_*keA 11

你考虑过DT吗?

library(DT)
datatable(df, rownames = FALSE) %>%
  formatStyle(columns = "inputval", 
              background = styleInterval(c(0.7, 0.8, 0.9)-1e-6, c("white", "lightblue", "magenta", "white"))) %>%
  formatStyle(columns = "outcome", 
              background = styleEqual(c(1, 4), c("magenta", "lightblue"))) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


raw*_*awr 6

我的答案真的很愚蠢..这是正确的方法:

此功能htmlTable通过css.cell参数内置:

css.cell元素允许您向表格单元格添加任何可能的CSS样式.如果您向量提供向量,则假定应在整个列中重复样式.如果提供与x参数大小相同的矩阵.如果ncol(x) + 1第一行将对应rowname样式.相应地,如果大小是nrow(x) + 1假设第一行是标题行.

所以基本上你只需要为每个单元格定义一个样式矩阵:

x <- head(cars)

## indices defining where the styles go
where <- rbind(c(2,2), c(2,1), c(5,2))
style <- c('background-color: red; color: white;',
           'border: solid 1px;',
           'font-weight: 900; color: blue;')

css.cell <- matrix('', nrow(x), ncol(x))
css.cell[where] <- style

#      [,1]                 [,2]                                  
# [1,] ""                   ""                                    
# [2,] "border: solid 1px;" "background-color: red; color: white;"
# [3,] ""                   ""                                    
# [4,] ""                   ""                                    
# [5,] ""                   "font-weight: 900; color: blue;"      
# [6,] ""                   ""                                

htmlTable(head(cars), css.cell = css.cell)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

除非你来回交换,否则很难判断,但是这张表和下面类似的表格中的间距略有不同.该inject_div示例看起来更集中.


有点晚了,但是@CyrusMohammadian对我的另一个答案发表了评论,由于评论/问题与此问题相同,我将在这里添加答案,而不是编辑我的答案,这是针对(稍微)不同的问题.

表可能变得复杂,每个人都有他们想要的不同功能.我认为Max不可能为他们提供内置的解决方案.

因此,我认为最简单的方法是(hackily)将一些html/css注入到你的表中(你可能也可以在运行后执行此操作htmlTable,即直接在html代码中执行此操作,但我认为这更容易):

#' Inject div
#' 
#' Inject an html division tag with style attribute.
#' 
#' @param x a matrix or data frame
#' @param where an \code{nx2} matrix of row and column indices or vector (of
#' the form c(row, col, row, col, ...)) specifying which cells to select
#' @param style vector of character string(s) applied to each cell, recycled
#' if necessary

inject_div <- function(x, where, style = 'background-color: lightgrey; border: solid 1px') {
  if (!all(sapply(style, nzchar)))
    return(x)
  where <- matrix(where, ncol = 2L, byrow = !is.matrix(where))
  style <- rep_len(style, nrow(where))
  if (length(where) > 0)
    x[where] <- sprintf('<div style=\'%s\'>%s</div>',
                      gsub(';*$', ';', style), x[where])
  x
}

library('htmlTable')
htmlTable(inject_div(head(cars), cbind(2,2)))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

htmlTable(inject_div(head(cars), where = c(2,2,2,1,5,2),
                     ## equivalently
                     # where = rbind(c(2,2), c(2,1), c(5,2))
                     style = c('background-color: red; color: white;',
                               'border: solid 1px;',
                               'font-weight: 900; color: blue;')))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述