简体   繁体   English

在R中使用库DT渲染DataTable时,如何使用行条件为单元格着色?

[英]How to color cells using a row condition while rendering DataTable using library DT in R?

I am rendering a 2 x 5 data table (all numeric rows) in a Shiny app using DT library. 我正在使用DT库在Shiny应用程序中呈现2 x 5数据表(所有数字行)。

I want to color cells by comparing each cell to the mean of its corresponding row. 我想通过将每个单元格与其对应行的平均值进行比较来为单元格着色。

I am unable to perform this using the current functions provided in the library. 我无法使用库中提供的当前功能执行此操作。 After some googling, I figured out that I would have to use JavaScript to achieve this. 经过一番谷歌搜索,我发现我必须使用JavaScript来实现这一点。

I have no experience of coding in JavaScript and require an example for doing this. 我没有使用JavaScript进行编码的经验,因此需要一个示例。

Requirement: Compare cell to the corresponding row mean, and color the cell if the value is less than the mean and green otherwise. 要求:将单元格与相应的行均值进行比较,如果该值小于均值,则为单元格上色,否则为绿色。 As a reproducible example, please refer to the following code chunk: 作为可重现的示例,请参考以下代码块:

set.seed(1)
x <- sample(1:10, size = 5, replace = T)

set.seed(1)
y <- sample(100:200, size = 5, replace = T)

## Main data frame, to be used in DT::datatable function
df <- data.frame(rbind(x, y))
df

##    X1  X2  X3  X4  X5
## x   3   4   6  10   3
## y 126 137 157 191 120

x_mean <- mean(x)
y_mean <- mean(y)

## Rendering data table
DT::datatable(
 df,
 options = list(
 searching = F,
 paging = F,
 ordering = F,
 info = F
 )
) %>% 
DT::formatStyle(1:5, backgroundColor = styleInterval(x_mean, c("red", 
 "green")))

When I run this code, the output I get is this: Actual Output This is performing column-wise comparisons to 'x_mean'. 当我运行此代码时,得到的输出是: 实际输出这是对'x_mean'进行按列比较。 However, I want to perform row-wise comparisons to 'x_mean', only for the first row. 但是,我只想对第一行执行与'x_mean'的逐行比较。 Cells of the second row should not be colored basis comparison to 'x_mean'. 第二行的单元格不应与'x_mean'进行比较。 Intended output is this: Intended Output 预期的输出是这样的: 预期的输出

Can this be done using any current function in DT library, or do I have to use JavaScript to achieve this (if so, what would be the JavaScript codes that I would have to insert?) ? 可以使用DT库中的任何当前函数来完成此操作,还是必须使用JavaScript来实现此目的(如果可以,那么我将要插入的JavaScript代码是什么?)?

library(DT)
set.seed(1)
x <- sample(1:10, size = 5, replace = T)
set.seed(1)
y <- sample(100:200, size = 5, replace = T)
df <- data.frame(rbind(x, y))


rowCallback <- c(
  "function(row, dat, displayNum, index){",
  "  var N = dat.length;",
  "  if(index == 0){ // only first row",
  "    var rowData = dat.slice(); rowData.shift();",
  "    var mean = rowData.reduce(function(a, b){ return a + b }, 0) / (N-1);",
  "    for(var j=1; j<N; j++){",
  "      var color = dat[j] < mean ? 'red' : 'green';",
  "      $('td:eq('+j+')', row).css('background-color', color);",
  "    }",
  "  }",
  "}"
)

datatable(
  df,
  options = list(
    searching = F,
    paging = F,
    ordering = F,
    info = F, 
    rowCallback = JS(rowCallback)
  )
)

A solution could be to create a loop to compare each value to your row mean, and then to colour your cell with the past command. 一种解决方案是创建一个循环,将每个值与行平均值进行比较,然后使用past命令为单元格着色。 You can find an example here : R to latex - Coloring numbers automatically 您可以在此处找到示例: R到乳胶-自动为数字着色

In this example the cell is coloured (in latex) with the command: \\\\cellcolor{red!25} . 在此示例中,单元格使用以下命令为乳胶上色: \\\\cellcolor{red!25} Change it according to the kind of extraction you want. 根据您想要的提取类型进行更改。

It is complicated to reply without any reproducible example. 没有任何可重复的示例,答复很复杂。 I still hope it helps. 我仍然希望能有所帮助。

EDIT 编辑

A quick and easy way is to select the row you want from the beginning ( df[1,] ): 一种快速简便的方法是从开头选择所需的行( df[1,] ):

datatable(df[1,]) %>% formatStyle(1:5,
                      backgroundColor = styleInterval(x_mean, c("red","green")))

We can make it a little more "automatic", replacing 1:5 by 1:length(df[1,]) and x_mean by mean(as.numeric(df[1,])) : 我们可以把它多一点的“自动”,替换1:51:length(df[1,])x_meanmean(as.numeric(df[1,]))

datatable(df[1,]) %>% formatStyle(1:length(df[1,]),
                      backgroundColor = styleInterval(mean(as.numeric(df[1,])), c("red","green")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM