我想知道我们是否可以在R Shiny中做到这一点 example
# using styleColorBar
datatable(df) %>% formatStyle(names(df),
background = styleColorBar(range(df), 'lightblue'),
backgroundSize = '98% 88%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center')
但是正负值有两种颜色,而且如果我们可以这样做,则负值被视为条形长度的绝对值
Ty,祝你有个美好的一天!
答案 0 :(得分:6)
您可以使用rowCallback
你可以看到
sss=datatable(df) %>% formatStyle(names(df),
background = styleColorBar(range(df), 'lightblue'),
backgroundSize = '98% 88%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center')
sss$x$options$rowCallback
为每列生成:
函数(行,数据){var value = data [1]; if(value!== null) $(this.api()。电池(行, 1).node())。css({' background':isNaN(parseFloat(value))|| value< = -1.311? '' :'线性渐变(90度,透明' +(2.199 - 值)/3.51 * 100 +'%,浅蓝' +(2.199 - 值)/3.51 * 100 + #&39;%)''背景大小':' 98% #88%及39;,'背景重复':'不重复','背景位置':'中心'}) ;
所以:
1)您需要计算范围变量:
rr=range(df)[2]-range(df)[1]
r1=range(df)[2]
r0=range(df)[1]
2)在rowCallback
datatable(
df,
options = list(
rowCallback=JS(paste0("function(row, data) {",
paste(lapply(1:ncol(df),function(i){
paste0("var value=data[",i,"];
if (value!==null){
if(value<0){
$(this.api().cell(row,",i,").node()).css({'background':isNaN(parseFloat(value)) || value <=",r0," ? '' : 'linear-gradient(90deg, transparent ' + (",r1," - value)/",rr," * 100 + '%, red ' + (",r1," - value)/",rr," * 100 + '%)','background-size':'98% 88%','background-repeat':'no-repeat','background-position':'center'});
}else{
$(this.api().cell(row,",i,").node()).css({'background':isNaN(parseFloat(value)) || value <=",r0," ? '' : 'linear-gradient(90deg, transparent ' + (",r1," - value)/",rr," * 100 + '%, lightblue ' + (",r1," - value)/",rr," * 100 + '%)','background-size':'98% 88%','background-repeat':'no-repeat','background-position':'center'});
}
} ")
}),collapse="\n"),
"}"))
)
)
这里我硬编码颜色(红色和浅蓝色)
如果你想要正面和负面的不同长度你需要不同的范围变量或使用abs
\ Math.abs
- 绝对值(对于JS和范围的var)
JS中的列的枚举从0开始,所以如果你没有rownames,你需要在0:(ncol(df)-1)
lapply
获得正确的结果
范围-1:1和abs值的色标
df = as.data.frame(cbind(matrix(round(runif(50, -1, 1), 3), 10), sample(0:1, 10, TRUE)))
rr=range(abs(df))[2]-range(abs(df))[1]
r1=range(abs(df))[2]
r0=range(abs(df))[1]
datatable(
df,
options = list(
rowCallback=JS(paste0("function(row, data) {",
paste(lapply(1:ncol(df),function(i){
paste0("var value=Math.abs(data[",i,"]);
var value2=data[",i,"];
if (value!==null){
if(value2<0){
$(this.api().cell(row,",i,").node()).css({'background':isNaN(parseFloat(value)) || value <=",r0," ? '' : 'linear-gradient(90deg, transparent ' + (",r1," - value)/",rr," * 100 + '%, red ' + (",r1," - value)/",rr," * 100 + '%)','background-size':'98% 88%','background-repeat':'no-repeat','background-position':'center'});
}else{
$(this.api().cell(row,",i,").node()).css({'background':isNaN(parseFloat(value)) || value <=",r0," ? '' : 'linear-gradient(90deg, transparent ' + (",r1," - value)/",rr," * 100 + '%, lightblue ' + (",r1," - value)/",rr," * 100 + '%)','background-size':'98% 88%','background-repeat':'no-repeat','background-position':'center'});
}
} ")
}),collapse="\n"),
"}"))
)
)