Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
突出显示R中每列数据帧的最大值_R_Colors_Datatable - Fatal编程技术网

突出显示R中每列数据帧的最大值

突出显示R中每列数据帧的最大值,r,colors,datatable,R,Colors,Datatable,我在R中有一个data.frame: p=structure(list(WSbin01 = c(214.98151752527, -46.9493685420515, 154.726947679253), WSbin02 = c(1093.46050365665, 420.318207941967, 927.97317496775), WSbin03 = c(2855.24990411661, 2035.57575481323, 2662.2595957214), WSbin04 = c(

我在
R
中有一个
data.frame

p=structure(list(WSbin01 = c(214.98151752527, -46.9493685420515, 
154.726947679253), WSbin02 = c(1093.46050365665, 420.318207941967, 
927.97317496775), WSbin03 = c(2855.24990411661, 2035.57575481323, 
2662.2595957214), WSbin04 = c(5863.91399544626, 4881.81544665127, 
5625.17650575444), WSbin05 = c(9891.70254019722, 8845.32506336827, 
9666.14583347469), WSbin06 = c(14562.1527820802, 13401.1727730953, 
14321.601249974), WSbin07 = c(19091.1307681137, 18003.2115315665, 
18903.0179613827), WSbin08 = c(24422.7094972645, 23694.5453703207, 
24357.8071162775), WSbin09 = c(30215.4088114124, 30214.3195264298, 
30310.242671113), WSbin10 = c(36958.2122031382, 37964.9044838778, 
37239.6908819524), WSbin11 = c(41844.810779792, 43701.2643596447, 
42343.7442683171), WSbin12 = c(37616.8187087318, 39348.3188777835, 
38178.9009247311), WSbin13 = c(20953.0973658833, 21720.1930292221, 
21251.8654076726), WSbin14 = c(7155.3786781173, 7262.61983182254, 
7233.60584469268), WSbin15 = c(2171.61052809769, 2120.97045661101, 
2173.49396732091), WSbin16 = c(779.72276608943, 745.52198490267, 
767.81436310063)), .Names = c("WSbin01", "WSbin02", "WSbin03", 
"WSbin04", "WSbin05", "WSbin06", "WSbin07", "WSbin08", "WSbin09", 
"WSbin10", "WSbin11", "WSbin12", "WSbin13", "WSbin14", "WSbin15", 
"WSbin16"), class = "data.frame", row.names = c(NA, -3L))
我想为每列的最大值设置背景色。 使用
DT::datatable
将返回表,但我不知道如何设置
formatStyle
参数,以不同颜色返回每列中的最大值


此外,我有一个向量z=c(1,1,1,1,1,1,1,1,3,2,2,2,2,2,2,3,1)。我想让每一列的背景色,比如如果
z[I]=1
column
I
应该是绿色,如果
z[I]=2
那么column
I
应该是红色,如果
z[I]=3
那么column
I
应该是蓝色

结合dt指南()和q()的部分内容,我得到以下结论:

colors <- apply(col2rgb(rainbow(n=ncol(p))),2,function(x)paste0("rgb(",paste(x,collapse=","),")"))
data <- datatable(p)
sapply(c(1:ncol(p)),function(x){
  data <<- data %>% formatStyle(colnames(p)[[x]],backgroundColor = styleEqual(max(p[[x]]), colors[x]))
})
data

colors如果我有一个像
z=c(1,1,1,1,1,1,1,1,1,1,1,1,3,2,2,2,2,2,3,1)这样的向量,我应该如何修改代码
,如果
z[I]=1
,那么列'I
z[I]=2'应该有颜色=红色,如果
z[I]=3
,那么column
i
应该有color=buleiz如果z是数据框中的一列,那么它看起来就像标题2下的示例不,它不在我的数据框中@user9112767-编辑上面的答案,以解决您的第二个问题。如果它不起作用,请告诉我。
z= c(1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2, 2, 2, 2, 3, 1)
colors <- apply(col2rgb(rainbow(n=max(z))),2,function(x)paste0("rgb(",paste(x,collapse=","),")"))
data <- datatable(p) 
sapply(c(1:ncol(p)),function(x){
  data <<- data %>% formatStyle(
    colnames(p)[[x]],
    backgroundColor = colors[z[x]]
  )
})
data