我使用了R,sweave(但是不是 knitr和xtable,来创建一个表,其中1列是标识符,其他三列是" flag& #34;列为空白或包含1(标志)的列。
我希望能够对最后三列进行整理,因此每个单元格都是绿色(如果是空白)或红色(如果它包含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获得的输出:
我做了几次尝试,不幸的是我在发布这个问题之前没有保存它们,我完全不记得任何尝试。
即使有人能指出我正确的方向,我也会非常感激。我已经能够找到关于R和LaTeX的信息,但没有关于R / Sweave 和 Latex的信息。
答案 0 :(得分:1)
我确信使用#Here is simulated data:
id <- c(rep(1,5),rep(2,4),rep(3,15),rep(4,8))
b <- c(rnorm(32,25,1))
c <- c(rnorm(32,30,1.5))
data <- cbind(id,b,c)
count.id <- table(id)
有一种聪明的方法可以做到这一点,但我非常擅长让xtable
做我想做的事情。这是一种使用xtable
的方法,可以为您提供所需的信息。 (这里棘手的部分是识别要遮蔽的表的坐标)。
pixiedust
可以在Sweave中使用相同的代码,但需要进行一些调整。等效代码是:
---
title: "Untitled"
author: "Author"
date: "May 25, 2016"
output: pdf_document
header-includes:
- \usepackage{amssymb}
- \usepackage{arydshln}
- \usepackage{caption}
- \usepackage{graphicx}
- \usepackage{hhline}
- \usepackage{longtable}
- \usepackage{multirow}
- \usepackage[dvipsnames,table]{xcolor}
- \makeatletter
- \newcommand*\vdashline{\rotatebox[origin=c]{90}{\$\dabar@\dabar@\dabar@\$}}
- \makeatother
---
```{r}
library(pixiedust)
id <- c("1_1", "1_2", "2_1")
a <- c(1,"","")
b <- c("", 1, "")
c <- c("", "", 1)
d <- data.frame(id, a,b,c)
cols <- apply(d[, -1], 1, function(x) which(x == 1)) + 1
rows <- apply(d[, -1], 2, function(x) which(x == 1))
dust(d,
float = FALSE,
hhline = TRUE) %>%
medley_all_borders() %>%
sprinkle(cols = 2:4,
bg = "green") %>%
sprinkle(rows = rows,
cols = cols,
fixed = TRUE,
bg = "red")
```
答案 1 :(得分:1)
\documentclass{article}
\usepackage[table]{xcolor}
\begin{document}
<<results='asis'>>=
library('xtable')
library('dplyr')
id <- c("1\\_1", "1\\_2", "2\\_1")
a <- c(1,"","")
b <- c("", 1, "")
c <- c("", "", 1)
d <- data.frame(id, a,b,c)
d %>% mutate_each(funs(paste0(ifelse(.=='', '\\cellcolor{red!25} ','\\cellcolor{green!25} '),.)), a:c) -> d
dx <- xtable(d)
align(dx) <- "|c|c|c|c|c|"
print(dx, hline.after=-1:3,
sanitize.text.function=identity)
@
\end{document}
答案 2 :(得分:1)
这是一个Sweave文件,它会有条件地将xtable单元格为红色或绿色。每个单元格的条件格式都是使用LaTeX colortbl和xcolor包完成的。
使用Sweave着色xtable单元格包括将转义字符(“\”),LaTeX“\ cellcolor”函数,HTML参数,颜色选择和单元格值粘贴在一起。在将data.frame转换为xtable之前,您需要具有类似于此的列:
c("\\cellcolor[HTML]{FF0600}{1}",
"\\cellcolor[HTML]{2DB200}{}",
"\\cellcolor[HTML]{2DB200}{}")
这是完整的Sweave文件。我正在使用knitr和pdfLaTeX在RStudio中编译它,我正在苏门答腊预览它。
\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:
\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}