We have an shiny app that encapsulate an DT that in turn contains a the green bar as shown at the picture bellow. Is there anyone that faced the same problem with the very last line? Other applications we have up and running works perfectly.
0
votes
1 Answers
3
votes
You probably used styleColorBar
. With this function, the bar for the cell containing the minimum value has length 0, so the output you get is the expected one. To have a bar with a length proportional to the value, you can use my function styleColorBar2
provided below. This function accepts two colors, one for the negative values and the other one for the positive values.
library(DT)
modelData <- data.frame(Channel = c("A", "B", "C"),
Current = c(2000, 3000, 4000),
Modified = c(2500, 3500, 3000),
New_Membership = c(500, 500, -1000),
stringsAsFactors = FALSE)
styleColorBar2 <- function (data, color1, color2)
{
M <- max(abs(data), na.rm = TRUE)
js <- c(
"value <= 0 ? ",
sprintf("'linear-gradient(90deg, transparent ' + (1+value/%f) * 100 + '%%, %s ' + (1+value/%f) * 100 + '%%)'",
M, color1, M),
" : ",
sprintf("'linear-gradient(90deg, transparent ' + (1-value/%f) * 100 + '%%, %s ' + (1-value/%f) * 100 + '%%)'",
M, color2, M)
)
JS(js)
}
datatable(
modelData , selection = 'none', editable = TRUE
) %>% formatStyle(
'New_Membership',
background = styleColorBar2(modelData$New_Membership, "red", "lightblue"),
backgroundSize = '100% 50%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center'
)