在R中为数据框各列最大值设背景色及按向量指定列颜色
Solution for DT Table Formatting Requirements
Let's walk through how to implement both of your requested formatting styles in DT::datatable—highlighting column maximums and setting column backgrounds based on your vector z.
Step 1: Prepare Your Data and Color Mapping
First, let's make sure we have your data and color rules set up:
library(DT) library(dplyr) # Your existing data frame p <- structure(list(WSbin01 = c(214.98151752527, -46.9493685420515, 154.726947679253), WSbin02 = c(1093.46050365665, 420.318207941967, 927.97317496775), WSbin03 = c(2855.24990411661, 2035.57575481323, 2662.2595957214), WSbin04 = c(5863.91399544626, 4881.81544665127, 5625.17650575444), WSbin05 = c(9891.70254019722, 8845.32506336827, 9666.14583347469), WSbin06 = c(14562.1527820802, 13401.1727730953, 14321.601249974), WSbin07 = c(19091.1307681137, 18003.2115315665, 18903.0179613827), WSbin08 = c(24422.7094972645, 23694.5453703207, 24357.8071162775), WSbin09 = c(30215.4088114124, 30214.3195264298, 30310.242671113), WSbin10 = c(36958.2122031382, 37964.9044838778, 37239.6908819524), WSbin11 = c(41844.810779792, 43701.2643596447, 42343.7442683171), WSbin12 = c(37616.8187087318, 39348.3188777835, 38178.9009247311), WSbin13 = c(20953.0973658833, 21720.1930292221, 21251.8654076726), WSbin14 = c(7155.3786781173, 7262.61983182254, 7233.60584469268), WSbin15 = c(2171.61052809769, 2120.97045661101, 2173.49396732091), WSbin16 = c(779.72276608943, 745.52198490267, 767.81436310063)), .Names = c("WSbin01", "WSbin02", "WSbin03", "WSbin04", "WSbin05", "WSbin06", "WSbin07", "WSbin08", "WSbin09", "WSbin10", "WSbin11", "WSbin12", "WSbin13", "WSbin14", "WSbin15", "WSbin16"), class = "data.frame", row.names = c(NA, -3L)) # Your z vector z <- c(1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2, 2, 2, 2, 3, 1) # Map z values to light background colors (we'll use darker shades for max values) col_colors <- case_when( z == 1 ~ "#d4edda", # Light green z == 2 ~ "#f8d7da", # Light red z == 3 ~ "#cce5ff" # Light blue ) # Calculate max value for each column (to use in highlighting) col_maxes <- sapply(p, max)
Step 2: Combine Both Formatting Rules in DT
We'll use columnDefs with a createdCell JavaScript function to first set the column's base background color, then override it for cells that contain the column's maximum value (using a darker shade of the same color for consistency):
datatable(p, options = list( columnDefs = list( # Loop through each column to apply both styles lapply(seq_along(col_colors), function(i) { # Pick a darker shade for max values based on z[i] max_shade <- case_when( z[i] == 1 ~ "#c3e6cb", z[i] == 2 ~ "#f5c6cb", z[i] == 3 ~ "#b8daff" ) list( targets = i - 1, # DT uses 0-based indexing for columns createdCell = JS( "function(cell, cellData, rowData, rowIndex, colIndex) {", // Set base column background color " $(cell).css('background-color', '", col_colors[i], "');", // Highlight max value with darker shade " if (cellData == ", col_maxes[i], ") {", " $(cell).css('background-color', '", max_shade, "');", " }", "}" ) ) }) ) ))
How This Works:
- Column Backgrounds: Each column gets a light background color matching your
zvector rules (green for z=1, red for z=2, blue for z=3). - Max Value Highlighting: For each column, the cell containing the maximum value gets a darker version of the column's base color—this makes the max value stand out while keeping the column's color category clear.
If You Want to Use Separate Styles:
- Only Column Backgrounds: Remove the
ifcondition in the JS function, leaving just the base background color line. - Only Max Value Highlighting: Skip the column background line and set a single highlight color (like
#ffffccfor yellow) for max values.
内容的提问来源于stack exchange,提问作者Haribo
相关产品推荐
相关产品推荐

