kableExtra icon indicating copy to clipboard operation
kableExtra copied to clipboard

Conflict in the color of a line in table kable knitr using striped and row_spec background

Open JoseCarlosMartinez opened this issue 3 years ago • 1 comments

hi, Great job for the community, amazing kableExtra I have not found a way to solve this problem, if it is a configuration problem, I sincerely apologize.

Describe the bug When for a kable knitr table I define the alternative row color with striped and at the same time formats with background color using row_spec, a conflict occurs when both are applied to a row, the gray color striped predominates, however, it should be the same background color set with row_spec the one that will predominate and be displayed.

To Reproduce

data.frame:


structure(list(Municipio = c("Albacete", "Alicante/Alacant", 
"Almería", "Ávila", "Badajoz", "Barcelona", "Bilbao", "Burgos", 
"Cáceres", "Cádiz", "Castellón/Castelló", "Ceuta", "Ciudad Real", 
"Córdoba", "Coruña (A)", "Cuenca", "Donostia-San Sebastián", 
"Girona", "Granada", "Guadalajara", "Huelva", "Huesca", "Jaén", 
"León", "Lleida", "Logroño", "Lugo", "Madrid", "Málaga", "Melilla", 
"Murcia", "Ourense", "Oviedo", "Palencia", "Palma", "Las Palmas", 
"Pamplona/Iruña", "Pontevedra", "Salamanca", "Santa Cruz de Tenerife", 
"Santander", "Segovia", "Sevilla", "Soria", "Tarragona", "Teruel", 
"Toledo", "Valencia", "Valladolid", "Vitoria-Gasteiz", "Zamora", 
"Zaragoza", "Media Capitales"), `2010` = c(74.0824400645256, 
63.952802450825, 92.3703293985148, 210.163242681775, 39.0333567856573, 
110.137066114095, 142.039342756104, 76.2034255266724, 46.07723908727, 
130.37397413889, 62.4622978028668, 169.738742228124, 84.2408635415966, 
88.0384659120308, 102.608135965893, 87.9393998825393, 144.925463866398, 
108.801567085082, 117.274353847312, 62.6228304431369, 83.7809515772554, 
53.8847805986972, 137.378924822331, 74.5977815419515, 163.115091602553, 
74.7229623976417, 84.3879409023403, 121.53760051866, 81.1910082549555, 
388.317614488255, 55.9903763722258, 86.1644355083599, 61.4824506229042, 
89.2828738331974, 83.0461735786953, 114.518906988636, 142.323225006076, 
47.1615203522767, 62.3741569447502, 60.7637207547509, 59.0125862800059, 
68.9574201765086, 85.3331349705623, 163.460263316432, 131.641555669691, 
56.775539002866, 72.0824296572876, 93.5007595391879, 69.6864028498805, 
207.114979496069, 63.0699901512167, 53.653761829361, 100.551334330353
), `2011` = c(67.370892234086, 58.5926992573184, 81.0128457202297, 
189.660259456345, 38.9212533236565, 114.161444453799, 120.874611652963, 
70.4254603879476, 45.5577044177383, 133.283097636358, 61.6898703043628, 
157.706532121006, 65.584835423407, 85.3988595474337, 100.280094826605, 
87.166287850731, 11.2042148400784, 109.889855668824, 125.694045206352, 
76.7220912223367, 84.9561241085698, 51.3630886867647, 112.318045658112, 
76.847321385524, 149.352725551959, 74.1636616636421, 60.2011964451519, 
92.9320889374029, 77.701949034382, 364.10979152862, 57.2494094793568, 
80.3621769041314, 61.3470282309409, 75.9256319894055, 83.8799361242284, 
113.994826434812, 118.353692530768, 35.1130457524272, 62.7229439246247, 
66.4233161770991, 44.8416260469873, 64.7212636725824, 89.6967740081733, 
192.479811188636, 124.810207256591, 51.8701354568125, 61.7740692833422, 
80.4108091269409, 57.7004175958805, 211.258285370802, 53.3828418161007, 
58.2218046759791, 90.4556192050316), `2012` = c(52.9978060786678, 
50.075816277138, 67.694071708028, 167.63493881015, 37.1458139489065, 
128.959264835346, 116.471576292058, 58.4673812435383, 37.7880463686917, 
124.869571675219, 55.7457955428292, 141.709288366779, 53.2628959837696, 
86.186775767012, 77.6128979954986, 64.3450648758592, 142.296003787371, 
95.3971518961296, 119.293732998071, 51.6581526596936, 89.9259988692047, 
51.4018045357197, 117.043075703969, 72.2185259720535, 139.975988386229, 
72.1993177403163, 58.3058340189118, 95.583955971297, 74.2437679514586, 
360.927031385362, 45.64538053354, 67.8129997118879, 60.5554376850332, 
69.6933939259588, 63.2530693392339, 105.16265961454, NA, 28.8105475061681, 
56.7077410423024, 60.1169064817723, 41.0317386042081, 64.0456029830063, 
90.1000770265749, 162.957925872419, 100.723839004434, 49.5721478195363, 
45.7379385615159, 73.7421345548713, 71.267311565613, NA, 51.5994770661852, 
45.757236810354, 87.2405015970483), `2013` = c(60.3657194559131, 
49.4534254981316, 78.9315318349533, 165.071626615816, 36.6375255774427, 
138.189256760362, NA, 56.1387765289201, 36.0634970028668, 127.390139442231, 
55.0908866997808, 141.290728439059, 55.3429030879367, 80.1026364449474, 
79.4457737177897, 54.2282946869375, 145.649209383378, 101.832179829791, 
106.155886308017, 47.3690737716558, 82.2518388127021, 51.5551987866763, 
102.767290748519, 68.5749012641557, 151.516785543134, 74.736052617825, 
63.6514950233392, 94.7938113060828, 75.4865949489779, 363.748699793258, 
48.5638066519717, 63.3313943389559, 60.1366861552541, 69.2925818051061, 
69.2052199356041, 114.468367419397, NA, 29.3179261822654, 56.2078220801455, 
67.1905833692332, 42.3163885548461, 58.6728890239187, 91.2936169410528, 
144.288890146656, 84.9566462241192, 48.4850437974472, 37.6193148947878, 
71.3025553860076, 67.8371843378084, NA, 47.0889209983689, 48.626300769497, 
87.6104333545098), `2014` = c(63.7084197069924, 49.4227069537172, 
70.8730990271579, 174.396738839021, 40.8916944265432, 145.549138665715, 
NA, 63.8337452749528, 40.1839400135622, NA, 57.2224579356999, 
170.809233901816, 55.1088082977588, 79.5955034888932, 80.6079030676851, 
54.7007842046719, 153.085271805121, 114.01450728707, 99.8778407005136, 
45.3739056378404, 76.4383440208678, 50.3756516030825, 98.7086423163583, 
71.779787959954, 150.438074955452, 77.3594353851621, 63.0459548498377, 
97.1723870139184, 75.1590995972927, 356.675478588079, 47.6910868477549, 
60.2105090500912, 60.2197437043327, 66.3453015789867, 69.104021368453, 
113.34530575516, NA, 33.3592960480312, 60.9819774118156, 71.1233508542033, 
46.1973652524241, 61.7417335711603, 83.7220794171179, 152.182584269663, 
88.3510984198065, 48.2130707778556, 36.9617263061895, 73.9299566645983, 
67.0433448163478, NA, 49.4343139872406, 54.4579260664987, 89.1777849421078
), `2015` = c(71.7725917232645, 48.574335033227, 76.5561520161892, 
NA, 37.5010408160542, 167.882904761756, 132.684437809475, 55.6027871259176, 
36.7751864208247, 125.508707125544, 59.5372515130862, 166.150563355209, 
57.8436484071641, 100.55359388078, 87.3504628695617, 42.6046240167424, 
135.26748262984, 102.356636402763, 98.4413013146734, 42.1597854684558, 
80.5074480241665, 48.5302398591091, 96.0864060834525, 75.3727487736373, 
156.108761458619, 77.1632920366846, 64.7490122689384, 101.528875751076, 
84.4926995414053, 374.791358665171, 54.4159245627874, 62.0454478447911, 
64.2855825032677, 65.7751758276274, 71.1467931089576, 119.809161457318, 
83.5175646020229, 35.9235532293825, 62.132064218304, 88.7411726550579, 
44.2827272256937, 67.1311447428311, 85.4259594626145, 157.655058721405, 
85.0671754980762, 49.5452000561956, 41.6746189892582, 72.1665926641049, 
71.0653052105099, NA, 43.5542985383278, 105.018534302424, 97.40457196348
), `2016` = c(63.3439368772691, 50.7720222978595, 74.5481562861476, 
162.842040356042, 36.979140623958, 167.638569774222, 145.335459344811, 
66.15157546657, 40.2270424990085, 128.014763158116, 63.6584632434645, 
178.887990037743, 60.6830638453021, 85.926707561641, 83.9394462615482, 
48.9338566658197, 139.078838517929, 102.213580072261, 101.929998722088, 
44.2895551995026, 83.1791983803998, 53.4446951149535, 110.393427410211, 
84.4211610086218, 82.0852780432013, 76.8105338821284, 73.7950728619693, 
106.384737041788, 88.1943190002267, 406.999765419757, 57.1609628279173, 
48.7622649278045, 73.0407519710564, 68.0468359932775, 70.1417089259435, 
114.81862851519, 93.0252252491695, 35.3553516093472, 66.8573263699646, 
102.21561131714, 42.4725720507831, 73.7106136976864, 89.7095458942375, 
161.372716295218, 90.8099579690909, 52.676309751434, 43.0090600174936, 
75.7182928520718, 64.788637818177, NA, 49.7537660755809, 112.921985908505, 
99.9711182806677), `2017` = c(66.255518296917, 55.4505971732305, 
67.3396882117212, 159.444527678894, 39.9208624778303, 191.885593614053, 
152.062916316537, 66.4060669160645, 41.5696869168135, 131.928027073733, 
70.8156033699513, 189.188525288669, 63.3576317305502, 99.9022751874716, 
92.1737310681322, 38.5831640425687, 139.352685196115, 91.7932459374022, 
108.74338544486, 42.8257587497772, 67.209950935465, 52.5816548264175, 
102.778140636216, 81.7958449372392, 87.2101693767431, 82.8515582299525, 
64.2431323026685, 116.244125268106, 95.4495291756444, 431.185251161171, 
57.9232964761993, 74.9121459540308, 75.0079722742974, 72.345751914009, 
75.6407281816124, 131.407698477426, 100.894645933306, 37.4719669533452, 
70.9266085324988, 119.707963542996, 56.4080210059843, 76.5130914290131, 
102.864800169414, 155.927878655384, 96.0587342879086, 57.0368704204712, 
43.3062216835242, 83.7300653585645, 68.8468635870744, NA, 57.9772431037523, 
112.835558277614, 108.522830152549), `2018` = c(69.9345114706732, 
54.6668854293271, 71.8805751558285, 181.106331234716, 44.3072417458314, 
214.179181617719, 156.464358092771, 70.3962592299953, 45.4426334471416, 
140.55641132169, 81.5296220331445, 190.140468382975, 64.6344999531729, 
108.344582908618, 99.4563863998366, 47.5381343582644, 144.99340947687, 
91.335109109768, 110.977560807552, 40.8479651395595, 82.5739391922805, 
58.0591296723405, 111.023540636541, 87.5520584746578, 96.7003161995125, 
89.2241733669506, 78.1986568732466, 127.196133695732, 95.2917635799421, 
430.719158061678, 61.6420758885644, 75.4283689872518, 95.246824288701, 
77.586684429409, 76.1457392087604, 127.944155110603, 98.1665755076206, 
44.2149079732374, 75.3660872494409, 118.261721794822, 51.9521717700123, 
79.3483423949848, 112.784929919807, 165.053829003886, 92.5101380206956, 
59.5908128099521, 39.8384217270592, 90.4243958843234, 76.9428500398172, 
NA, 57.016256975108, 118.480394088892, 116.427447278279), `2019` = c(79.6128362824455, 
57.7707025653429, 79.5972663486675, 181.308866029371, 44.9166119228676, 
217.073569321624, 162.638647370712, 77.1900741094636, 45.9774171400037, 
150.339926310255, 84.5209088791577, 216.651511376906, 69.7308898135017, 
115.110930454619, 102.462176947715, 50.1939875662827, 230.875413280687, 
109.11500471272, 122.021490049987, 44.0954648251447, 92.3856052706682, 
57.9014362342844, 113.965265975805, 89.7780146094624, 104.329687670917, 
61.7694532738725, 85.9764999592983, 142.723899993448, 114.664026179231, 
449.351418132205, 65.6847462151799, 71.6800458981498, 83.9982363009022, 
79.2833163291333, 78.2415773256583, 144.066310061196, 98.5252406361423, 
46.3581870189934, 84.9626707712788, 136.34329725245, 55.2160207837069, 
81.9869586252274, 123.393246436206, 189.591221127976, 93.0933471360071, 
65.2912828085818, 43.7564548207322, 98.9957289673267, 90.7221160342078, 
NA, 67.2139240465101, 116.809049877259, 125.540014778074)), row.names = c(NA, 
-53L), class = "data.frame")

code kable kableExgra knitr:


dfam %>%
 kable(caption="", 
  row.names=F, digits = 2, 
  format.args = list(big.mark = ".", decimal.mark = ","),
  centering = FALSE, 
  booktabs = F, 
  vline="", 
  linesep = "",  
  longtable = TRUE) %>%  
 kable_styling(bootstrap_options = c("hover", "condensed", "responsive"), 
  latex_options = c("striped", "condensed", "HOLD_position", "repeat_header"),
  repeat_header_text = "(continúa)",
  full_width = F, fixed_thead = T) %>%  
 row_spec(0, bold=T, monospace=F, color="white", background="#7B7B7B") %>%
 row_spec(grep(paste("^","Murcia","$",sep=""),dfam$Municipio), bold=T, color="white", background="#C31A00") %>%
 row_spec(grep(paste("^","Media Capitales","$",sep=""),dfam$Municipio), bold=T, color="white", background="#8673a1")

JoseCarlosMartinez avatar Jun 27 '21 21:06 JoseCarlosMartinez

I'm experiencing the same problem. When I use slightly edited code from an old answer to a different question on Stackoverflow, it illustrates the problem quite nicely.

DF <- data.frame(V1 = sample(letters,10,T), V2 = abs(rnorm(10)), V3 = abs(rnorm(10)))
paint <- function(x) ifelse(x < 0.2, "yellow", "red")

DF[,-1] = lapply(DF[,-1], formatC, format = 'f', digits = 2)
DF[,-1] = lapply(DF[,-1], function(x) 
  ifelse(x < 0.1, x, cell_spec(x, background = paint(x), format = "latex")))

kable(DF, caption = "Highlighted numbers near zero", 
  digits = 2, format = "latex", booktabs = T, escape = F, longtable = T) %>%
  kable_styling(latex_options = c("striped", "hold_position", 
    "repeat_header", font_size = 6)) 

The PDF output looks like this for me, where the grey striped background overwrites the conditional formatting. This is using cell_spec, but the result is the same when I use column_spec.

image

I'm using kableExtra 1.3.4 and knitr 1.36

DavidClaszen avatar Oct 08 '21 03:10 DavidClaszen