2016-11-30 4 views
3

このテーブルは.RMDファイルにあり、レンダリングしたいのですが、PDFで条件付き書式を設定しています。現在、私はpandocを使用しています。これはxtableでどのようにすることができますか?xtableでセルの色付けを行う方法

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 
---------------------------- 

私は、「グループA」等の条件付き書式で「グループB」列のセルに色をすることができます方法:

>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" 

答えて

3

関連するテーブルエントリをラテックスコード(from here)でラップし、xtableの結果をサニタイズすることができます。

例:

--- 
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) 
``` 

私はこのエラーを取得しています

enter image description here

+0

生成! Extra alignmentタブが\ crに変更されました。 \ endtemplate – user3022875

+0

@ user3022875;私はちょうどコピーし、新しい.Rmdファイルに私の答えからのコードを貼り付け、それは期待どおりに走った。あなたは何かを追加しましたか?例えば、列の数を変更したり、整列させたりしましたか? – user20650

+0

何もhaventが追加されていません – user3022875

関連する問題