admin管理员组文章数量:1024700
I am trying to color the positive and negative values of a certain column (third) of a DT::Datatable
with green and red colors respectively based on this code chunk, but I am not a javascript user. Is there any way to set this?
library(DT)
datatable(head(iris)) %>%
formatStyle(1:4, color = JS("value % 1 === 0 ? 'red' : ''"))
I am trying to color the positive and negative values of a certain column (third) of a DT::Datatable
with green and red colors respectively based on this code chunk, but I am not a javascript user. Is there any way to set this?
library(DT)
datatable(head(iris)) %>%
formatStyle(1:4, color = JS("value % 1 === 0 ? 'red' : ''"))
Share
Improve this question
edited Aug 23, 2019 at 13:24
Joris C.
6,2643 gold badges14 silver badges29 bronze badges
asked Aug 23, 2019 at 11:09
firmo23firmo23
8,4863 gold badges48 silver badges162 bronze badges
1
- May be useful for you: stackoverflow./questions/47508736/… – Saurabh Chauhan Commented Aug 23, 2019 at 11:14
1 Answer
Reset to default 8You can use DT::styleInterval
for this:
library(DT)
## data (iris dataset contains no negative values)
dat <- data.frame(
letters = LETTERS[1:26],
numbers = sample(c(-1, -0.5, 0, 0.5, 1), 26, replace = TRUE)
)
datatable(dat) %>%
formatStyle(
columns = "numbers",
color = styleInterval(cuts = 0, values = c("red", "green")),
fontWeight = "bold"
)
NB: If zero values should be ignored, you can set a black color to a small region around zero:
eps <- 1E-5
datatable(dat) %>%
formatStyle(
columns = "numbers",
color = styleInterval(cuts = c(-eps, eps), values = c("red", "black", "green")),
fontWeight = "bold"
)
I am trying to color the positive and negative values of a certain column (third) of a DT::Datatable
with green and red colors respectively based on this code chunk, but I am not a javascript user. Is there any way to set this?
library(DT)
datatable(head(iris)) %>%
formatStyle(1:4, color = JS("value % 1 === 0 ? 'red' : ''"))
I am trying to color the positive and negative values of a certain column (third) of a DT::Datatable
with green and red colors respectively based on this code chunk, but I am not a javascript user. Is there any way to set this?
library(DT)
datatable(head(iris)) %>%
formatStyle(1:4, color = JS("value % 1 === 0 ? 'red' : ''"))
Share
Improve this question
edited Aug 23, 2019 at 13:24
Joris C.
6,2643 gold badges14 silver badges29 bronze badges
asked Aug 23, 2019 at 11:09
firmo23firmo23
8,4863 gold badges48 silver badges162 bronze badges
1
- May be useful for you: stackoverflow./questions/47508736/… – Saurabh Chauhan Commented Aug 23, 2019 at 11:14
1 Answer
Reset to default 8You can use DT::styleInterval
for this:
library(DT)
## data (iris dataset contains no negative values)
dat <- data.frame(
letters = LETTERS[1:26],
numbers = sample(c(-1, -0.5, 0, 0.5, 1), 26, replace = TRUE)
)
datatable(dat) %>%
formatStyle(
columns = "numbers",
color = styleInterval(cuts = 0, values = c("red", "green")),
fontWeight = "bold"
)
NB: If zero values should be ignored, you can set a black color to a small region around zero:
eps <- 1E-5
datatable(dat) %>%
formatStyle(
columns = "numbers",
color = styleInterval(cuts = c(-eps, eps), values = c("red", "black", "green")),
fontWeight = "bold"
)
本文标签: javascriptColor differently positive and negative values of DTDatatableStack Overflow
版权声明:本文标题:javascript - Color differently positive and negative values of DT::Datatable - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745608257a2158852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论