简体   繁体   English

尝试对颜色代码编织xtable时出错

[英]Error Attempting to Color Code Sweave xtable

I'm trying to use R and Sweave to color code a table as described in this question . 我正在尝试使用R和Sweave对表进行颜色编码,如本问题所述 I've got the following code: 我有以下代码:

<<>>=
Overall <- data$FLAG_OVERALL_HOSP
DC_Info <- data$FLAG_DC_INFO
Care_Trans <- data$FLAG_CARE_TRANS
Dept <- data$DEPT_DSC
HOSP <- data$ALPHA_CODE
Flag <- data.frame(HOSP,Dept, Overall, DC_Info, Care_Trans)
@
<<results=tex>>=
color_cells <- function(df, var){
 out <- ifelse(df[, var]=="", 
                  paste0("\\cellcolor[HTML]{2DB200}{", df[, var], "}"),
                  paste0("\\cellcolor[HTML]{FF0600}{", df[, var], "}"))
}
Flag$Overall <- color_cells(df = Flag, var= "Overall")
Flag$DC_Info <- color_cells(df = Flag, var= "DC_Info")
Flag$Care_Trans <- color_cells(df = Flag, var= "Care_Trans")
@

<<results=tex>>=
Flagx <- xtable(Flag)
align(Flagx) <- "|c|l|l|c|c|c|"
print(Flagx[1:40,], hline.after=c(-1:40), sanitize.text.function=identity)
@
<<results=tex>>=
Flagx <- xtable(Flag)
align(Flagx) <- "|c|l|l|c|c|c|"
print(Flagx[41:62,], hline.after=c(-1:22), sanitize.text.function=identity)
@

But I'm getting the following message: 但是我收到以下消息:

我收到错误消息

What am I doing wrong here? 我在这里做错了什么?

Edit: Here is a small portion of my data 编辑:这是我数据的一小部分

ALPHA_CODE <- c(AF, DX, DX)
Dept <- c(MSN, ICU, PEDS)
OVERALL<- c(NA,NA,1)
DC_Info <- c(NA,NA,NA)
Care_Trans <- c(1,NA,NA)
Flag <- data.frame(HOSP,Dept, Overall, DC_Info, Care_Trans)

Now that I'm at a computer I see the issue. 现在我在电脑上,我看到了这个问题。 Your column names have underscores in them, and LaTeX assumes that you mean to subset a character, and that tends only to work in math mode. 您的列名称中带有下划线,并且LaTeX假定您要对字符进行子集化,并且仅在数学模式下有效。 (ie, between $ characters). (即$字符之间)。

In my experience, when I get the "Missing $ inserted" error, it means either 以我的经验,当我收到“ Missing $ insert”错误时,这意味着

  1. I've not interpreted my output as LaTeX ( results = tex is usually what I've forgotten. or 我没有将我的输出解释为LaTeX( results = tex通常是我忘记的东西。
  2. I've failed to escape a special character that is assumed to be in math mode (such as $ , _ , and others. A good list of these is in the documentation for Hmisc::latexTranslate . 我未能转义一个假定处于数学模式的特殊字符(例如$_和其他Hmisc::latexTranslate 。这些文件的很好列表在Hmisc::latexTranslate的文档中。

The solution in your case is to sanitize your column names. 您的解决方案是清理您的列名。

print(Flagx[41:62,], 
    hline.after=c(-1:22), 
    sanitize.text.function=identity,
    sanitize.colnames.function = Hmisc::latexTranslate)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM