Reputation: 195
I am trying to apply background colour to a datatable of 'date' information: green = up to date, yellow = out of date, red = REALLY out of date. Basically like the 'style a full table' heatmap example found here: https://rstudio.github.io/DT/010-style.html
My code looks approximately like this;
library(wakefield)
library(shiny)
library(DT)
backlogTbl <- r_data_frame(n = 5,
date_stamp,
date_stamp,
date_stamp,
date_stamp) %>%
r_na(prob=.25)
backlogDT <- datatable(backlogTbl) %>%
formatStyle(names(backlogTbl),
backgroundColor = styleInterval(Sys.Date() - days(c(7,2)),
c("red", "yellow", "green")))
When I run this every non-null background is green instead of green/yellow/or red depending on the value but when I compare individual cells to my breakpoints i.e.
backlogTbl[1,1] < Sys.Date() - days(c(7,2))
I can see that the values are within the intervals I've defined.
If I pass the iris dataset through my code it formats fine which makes me suspect that the problem is that it's date time data? I feel like I'm missing something obvious...
Thank you in advance!
Upvotes: 2
Views: 3008
Reputation: 1547
I don't think this is currently supported, and I created a GitHub issue for it.
One workaround is to create a column that holds integer versions of the dates and have your style based on that. Then hide the integer column
library(shiny)
library(DT)
library(tidyverse)
library(lubridate)
ui <- fluidPage(
titlePanel("DateDemo"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
DTOutput('tbl'),
width = 10,
height = "800px")
)
)
server <- function(input, output) {
output$tbl <-
renderDT({
splitDate <- as.Date("2018-02-02")
data_frame(ExampleDate = as.Date(c("2018-01-01","2019-01-01"))) %>%
mutate(ExampleDateInt = as.integer(ExampleDate)) %>% #make column with integer dates
datatable(options=list(columnDefs = list(list(visible=FALSE, targets=c(2))))) %>% #hide the integer column
formatStyle(columns = "ExampleDate",
valueColumns = "ExampleDateInt",
backgroundColor = styleInterval(as.integer(splitDate),c("#FB717E","#89EC6A"))) #format based on the integer column
})
}
shinyApp(ui = ui, server = server)
Upvotes: 2