1

I'm trying to use R and Sweave to color code a table as described in this question. 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:

Error Message I'm getting

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)
Community
  • 1
  • 1
Amanda R.
  • 287
  • 1
  • 2
  • 17

1 Answers1

2

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. (ie, between $ characters).

In my experience, when I get the "Missing $ inserted" error, it means either

  1. I've not interpreted my output as LaTeX (results = tex is usually what I've forgotten. or
  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.

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)
Benjamin
  • 16,897
  • 6
  • 45
  • 65
  • Am I supposed to use that exact code chunk above? I tried and I got the the following message: `! LaTeX Error: File `Hmisc.sty' not found.` – Amanda R. Jun 01 '16 at 16:39
  • You'll need to have the `Hmisc` package installed (`install.packages("Hmisc")`), but I've never had to do anything beyond that. – Benjamin Jun 01 '16 at 16:42
  • I'm not sure what to do next. running a very minimal example on my machine produces the document, and at this point, your issue appears to be more on the LaTeX side than the R side. Are you using any other `Hmisc::latex`-type functions in your document? – Benjamin Jun 01 '16 at 17:02
  • I figured out what was wrong and fixed it. Thank you! – Amanda R. Jun 01 '16 at 17:05
  • Just out of curiosity, what was it. I'm curious for my own part, and having it documented for future questioners is an added benefit. – Benjamin Jun 01 '16 at 17:10
  • I thought I needed to have `\usepackage{Hmisc}` at the beginning, but I took that out, and ran `library(Hmisc)` in the console and then it worked. – Amanda R. Jun 01 '16 at 17:15
  • I just realized that it is only giving the assigned background color to the cells that are supposed to have a red background--none of them have a green background as needed. Do you have any ideas as to why this is? – Amanda R. Jun 01 '16 at 17:17