xtable的颜色代码单元格 [英] Color Code Cells of xtable

查看:193
本文介绍了xtable的颜色代码单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用R,sweave(但是不是 knitr和xtable)创建了一个表,其中1列是标识符,其他三列是"flag"列,它们分别是空白或包含1(标志).

I've used R, sweave (but not knitr, and xtable, to create a table, where 1 column is an identifier, and the other three columns are "flag" columns that are either blank or contain a 1 (the flag).

我希望能够对最后三列进行阴影处理,以便每个单元格为绿色(如果为空)或红色(如果为1).

I want to be able to shad the last three columns so each cell is either green (if it is blank) or red (if it contains a 1).

<<xtable3, results=tex>>=
id <- c("1_1", "1_2", "2_1")
a <- c(1,"","")
b <- c("", 1, "")
c <- c("", "", 1)
d <- data.frame(id, a,b,c)
dx <- xtable(d)
align(dx) <- "|c|c|c|c|c|"
print(dx, hline.after=-1:3)
@

这是我通过Sumatra PDF Viewer获得的输出:

This is the output I get via Sumatra PDF Viewer:

我做了几次尝试,很遗憾,在发布此问题之前我没有保存它们,我无法确切地回忆起任何尝试.

I have made several attempts, unfortunately I didn't save them before posting this question and I cannot recall any of the attempts exactly.

即使有人能指出正确的方向,我也将非常感激.我已经能够找到有关R和LaTeX的信息,但是找不到有关R/Sweave Latex的信息.

Even if someone could just point me in the right directions I would really appreciate it. I've been able to find information on R and LaTeX, but not information that is for R/Sweave and Latex.

推荐答案

这是一个Sweave文件,该文件将有条件地将xtable单元格着色为红色或绿色.每个单元格的条件格式是通过LaTeX colortbl和xcolor软件包完成的.

Here's a Sweave file that will conditionally color xtable cells red or green. The conditional formatting of each cell is accomplished with the LaTeX colortbl and xcolor packages.

使用Sweave为xtable单元上色包括将转义符("\"),LaTeX"\ cellcolor"函数,HTML参数,颜色选择和单元格值粘贴在一起.在将data.frame转换为xtable之前,您将需要具有如下所示的列:

Coloring xtable cells with Sweave involves pasting together an escape character ("\"), the LaTeX "\cellcolor" function, an HTML argument, your color choice, and your cell value. You'll want to have columns that look something like this before you convert the data.frame to xtable:

c("\\cellcolor[HTML]{FF0600}{1}", 
"\\cellcolor[HTML]{2DB200}{}", 
"\\cellcolor[HTML]{2DB200}{}")

这是完整的Sweave文件.我正在使用knitr和pdfLaTeX在RStudio中对其进行编译,并在苏门答腊中对其进行预览.

Here is the full Sweave file. I am compiling this in RStudio using knitr and pdfLaTeX, and I'm previewing it in Sumatra.

\documentclass{article}

\usepackage[table]{xcolor}

\begin{document}

<<xtable1, results = 'asis', echo = FALSE, warning = FALSE>>=
library(xtable)

# build your data.frame
id <- c("1_1", "1_2", "2_1")
a <- c("1", "", "")
b <- c("", "1", "")
c <- c("", "", "1")
d <- data.frame(id, a, b, c)

# define function that will color blank cells green and not blank cells red
color_cells <- function(df, var){
  out <- ifelse(df[, var]=="", 
                      paste0("\\cellcolor[HTML]{2DB200}{", df[, var], "}"),
                      paste0("\\cellcolor[HTML]{FF0600}{", df[, var], "}"))
}

# apply coloring function to each column you want
d$a <- color_cells(df = d, var= "a")
d$b <- color_cells(df = d, var= "b")
d$c <- color_cells(df = d, var= "c")

# convert data.frame to xtable and print it with sanitization
dx <- xtable(d)
align(dx) <- "|c|c|c|c|c|"
print(dx, 
      hline.after=-1:3,
      sanitize.text.function = function(x) x)
@

\end{document}

以下是用于使用Sweave(而不是knitr)进行编译的代码:

Here is the code for compiling with Sweave, not knitr:

\documentclass{article}

\usepackage[table]{xcolor}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<xtable1, results = tex, echo = FALSE, warning = FALSE>>=
library(xtable)

# build your data.frame
id <- c("1_1", "1_2", "2_1")
a <- c("1", "", "")
b <- c("", "1", "")
c <- c("", "", "1")
d <- data.frame(id, a, b, c)

# define function that will color NA cells green and not-NA cells red
color_cells <- function(df, var){
  out <- ifelse(df[, var]=="", 
                      paste0("\\cellcolor[HTML]{2DB200}{", df[, var], "}"),
                      paste0("\\cellcolor[HTML]{FF0600}{", df[, var], "}"))
}

# apply coloring function to each column you want
d$a <- color_cells(df = d, var= "a")
d$b <- color_cells(df = d, var= "b")
d$c <- color_cells(df = d, var= "c")

# convert data.frame to xtable and print it with sanitization
dx <- xtable(d)
align(dx) <- "|c|c|c|c|c|"
print(dx, 
      hline.after=-1:3,
      sanitize.text.function = function(x) x)
@

\end{document} 

这篇关于xtable的颜色代码单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆