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
Add a ment  | 

1 Answer 1

Reset to default 8

You 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
Add a ment  | 

1 Answer 1

Reset to default 8

You 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