library(magrittr)
library(DT)
dat <- data.frame(x1 = c(30, 20, 5), x2 = c(35, 15, 20))
dat <- transform(dat, diff1 = x2 - x1) %>%
transform(diff2 = (x2 - x1) / x1) %>%
transform(diff3 = paste0(diff1, "(", scales::percent(diff2, accuracy = 0.01), ")"))
datatable(dat) %>%
formatPercentage(c("diff2"), digits = 2) %>%
formatStyle("diff2", color = DT::styleInterval(0, c("red", "green")))
当前的状况是
我想要的是 diff3 这一列加上红绿色