I am looking to color format each numeric column so that it shows a blue range bar depending on the range of each column. Here is a photo on what I am trying to achieve.
#Here is the the table I have so far.
#I am adding filters to the columns. This works great
library(DT)
library(magrittr)
df = datatable(iris, filter = 'top', options = list(pageLength = 5, autoWidth = TRUE))
#I try to add the blue bars here to the first 2 numeric columns
df %>%formatStyle(names(df)[1:2],
background = styleColorBar(range(df[,1:2]), 'lightblue'),
backgroundSize = '98% 88%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center')
Error in df[, 1:2] : incorrect number of dimensions
如果有一个功能可以自动将蓝色条放置在所有数字列上,那就太好了。提前致谢
您可以改进以下几点:
- 以编程方式定义数字列(通过
sapply(iris, is.numeric)
结果)。
- 要格式化样式,您需要传递原始表(
iris
代替df
). df
由于行名称的原因,可能有不同的列。
- To
styleColorBar
还传递原始表而不是df
.
Code:
library(magrittr)
library(DT)
# Specify numeric columns
foo <- sapply(iris, is.numeric)
datatable(iris, filter = 'top', options = list(pageLength = 5, autoWidth = TRUE)) %>%
formatStyle(names(iris)[foo],
background = styleColorBar(range(iris[, foo]), 'lightblue'),
backgroundSize = '98% 88%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center')
Result:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)