Try:
color_from_middle <- function (data,low,high, colorlow,colorhigh,colordefault)
{
max_val=max(abs(data))
JS(sprintf("isNaN(parseFloat(value)) ||
value < %s ? 'linear-gradient(90deg, transparent, transparent ' + (50 + value/%s * 50) + '%%, %s ' + (50 + value/%s * 50) + '%%,%s 50%%,transparent 50%%)' :
value > %s ? 'linear-gradient(90deg, transparent, transparent 50%%, %s 50%%, %s ' + (50 + value/%s * 50) + '%%, transparent ' + (50 + value/%s * 50) + '%%)':
value < 0 ? 'linear-gradient(90deg, transparent, transparent ' + (50 + value/%s * 50) + '%%, %s ' + (50 + value/%s * 50) + '%%,%s 50%%,transparent 50%%)':
'linear-gradient(90deg, transparent, transparent 50%%, %s 50%%, %s ' + (50 + value/%s * 50) + '%%, transparent ' + (50 + value/%s * 50) + '%%)'",
low,max_val,colorlow,max_val,colorlow,high,colorhigh,colorhigh,max_val,max_val,max_val,colordefault,max_val,colordefault,colordefault,colordefault,max_val,max_val))
}
datatable(data) %>%
formatStyle('value',background=color_from_middle(data$value,-5,5,'red','blue','grey'))
This relies on JavaScript conditional operator : condition ? true : false
