2016-11-30 5 views
3

Ich habe diese Tabelle in einer .RMD-Datei und ich würde gerne in PDF mit bedingter Formatierung rendern. Momentan verwende ich Pandoc. Wie kann dies mit xtable getan werden?Wie kann xtable Zell Färbung

table = data.frame(category = c("A","B","C"), groupA = c(.2,.3,.5), groupB= c(.6,.7,.9)) 
table 
pandoc.table(table,split.table = Inf,keep.line.breaks = TRUE) 

---------------------------- 
category groupA groupB 
---------- -------- -------- 
    A  0.2  0.6 

    B  0.3  0.7 

    C  0.5  0.9 
---------------------------- 

Wie kann ich die Zellen der "groupA" und "groupB" Spalten mit der bedingten Formatierung Farbe wie:

>0 and <= .2 = "green" 
>.2 and <= .3 = "red" 
>.3 and <= .4 = "blue" 
>.4 and <= .5  = "orange" 
>.5 and <= .6  = "yellow" 
>.6 and <= .7  = "black" 
>.7 and <= .8  = "brown" 
>.8 = "white" 

Antwort

3

Sie können die relevanten Tabelleneinträge in den Latex-Code (from here) umbrechen und anschließend die Ergebnisse der Xxtabelle bereinigen.

Beispiel:

--- 
header-includes: 
    - \usepackage{xcolor, colortbl} 
output: 
    pdf_document 
--- 

```{r, results="asis"} 

library(xtable) 
# Your data 
tab = data.frame(category = c("A","B","C"), groupA = c(.2,.3,.5), groupB= c(.6,.7,.9)) 

# Function to cut your data, and assign colour to each range 
f <- function(x) cut(x, c(0, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, Inf), 
         labels=c("green", "red", "blue", "orange", "yellow", "purple", "brown", "white"), 
         include.lowest = FALSE, right = TRUE) 

# Apply function to columns: this overwrites your data 
tab[c("groupA", "groupB")] <- lapply(tab[c("groupA", "groupB")], function(x) 
              paste0("\\cellcolor{", f(x), "}", x)) 
# Sanitise output 
print(xtable(tab), sanitize.text.function = identity) 
``` 

die

enter image description here

+0

ich diese Störung erhalte produziert! Die Registerkarte "Extra Ausrichtung" wurde in \ cr geändert. \ endtemplate – user3022875

+0

@ user3022875; Ich habe einfach den Code aus meiner Antwort kopiert und in eine neue .RMD-Datei eingefügt, und er lief wie erwartet. Haben Sie etwas hinzugefügt, zum Beispiel die Anzahl der Spalten geändert oder ausrichten? – user20650

+0

keine havent nichts hinzugefügt – user3022875