如果您改为使用 rgb 颜色空间着色,您可以这样做:
library(shiny)
color.min <- 0
color.max <- 250
n <- 100
# Simulate data
random.data <- data.frame("l"=runif(n,min=color.min,max=color.max),
"a"=runif(n,min=color.min,max=color.max),
"b"=runif(n,min=color.min,max=color.max),
"color"=rep(NA,n))
server <- shinyServer(function(input, output, session) {
observe({
output$table <- renderTable({
random.data
})
})
})
ui <- shinyUI(fluidPage(
# Add jQuery script
tags$head(
tags$script(
HTML("
function color(){
if(document.querySelector('#table')!=null){
// Select each table row in an array and loop over that with jQuery each
$('#table > table > tbody').find('tr').each(function(index, value) {
// Get values for rgb and round to integers
var vals = [];
$(this).children('td').slice(1, 4).each(function(index, value) {
vals[index] = parseInt( $(this).html() );
});
// Color 5:th child the selected rgb color
$(':nth-child(5)',this).css('background','rgb('+String(vals[0])+','+String(vals[1])+','+String(vals[2])+')');
})
}
else{
setTimeout(function() { color(); }, 100);
}
}
color();
")
)
),
fluidRow(
column(10, uiOutput("table")),
column(2,actionButton("color","color"))
)
))
shinyApp(ui = ui, server = server)