I have a small header I want to print at the top of a report. It should look like this in the end:
The report is generated by an RMarkdown/Knitr report. I am mixing in Latex to get the formatting I need. The data is based on a table I generate by pulling results from a REST Query. The dataframe in R looks like:
I coded up the LaTex code to get the result I wanted and that appears as such:
\begin{spacing}{.7}
\begin{center}
ECC \begin{tabular}{|C|C|} \hline \cellcolor{red} \textbf{\scriptsize \textcolor{white}{TOUCH}} & \textbf{\scriptsize NO TOUCH} \\ \hline \end{tabular} \hspace{.9em}
DOC \begin{tabular}{|C|C|} \hline \textbf{\scriptsize TOUCH} & \textbf{\scriptsize NO TOUCH} \\ \hline \end{tabular} \hspace{.9em}
CCC \begin{tabular}{|C|C|} \hline \textbf{\scriptsize TOUCH} & \textbf{\scriptsize NO TOUCH} \\ \hline \end{tabular} \hspace{.9em}
GEN \begin{tabular}{|C|C|} \hline \textbf{\scriptsize TOUCH} & \textbf{\scriptsize NO TOUCH} \\ \hline \end{tabular} \hspace{.9em}
IT \begin{tabular}{|C|C|} \hline \textbf{\scriptsize TOUCH} & \textbf{\scriptsize NO TOUCH} \\ \hline \end{tabular} \hspace{.9em}
\end{center}
\end{spacing}
So far, so good. But the coloring of the text and the cell colors need to be dynamic. The criteria are as follows:
- If the environment is a "Touch" environment, that cell should be colored green and the text white.
- If the environment is a "No Touch" environment, that cell should be colored red and the text white.
- Cells not colored in each section should have a white background and black text.
Not so hard, I created a code chunk in the report that is wrapped inside of the \being{center}
and \begin{spacing}{.7}
items, that looks like this:
```{r echo = FALSE, warning = FALSE, message=FALSE, results='asis'}
# Add row to data frame to build table options
touchNoTouchDF$tableOption <- NA
for(i in 1:nrow(touchNoTouchDF)) {
if(strsplit(touchNoTouchDF[i,1], ":")[[1]][2] == "Touch") {
touchNoTouchDF[i,2] <- paste(strsplit(touchNoTouchDF[i,1], ":")[[1]][1],"\\begin{tabular}{|C|C|} \\hline \\cellcolor{OliveGreen!85} \\textbf{\\scriptsize \\textcolor{white}{TOUCH}} \& \\textbf{\\scriptsize NO TOUCH} \\newline \\hline \\end{tabular} \\hspace{.9em}")
} else {
touchNoTouchDF[i,2] <- paste(strsplit(touchNoTouchDF[i,1], ":")[[1]][1],"\\begin{tabular}{|C|C|} \\hline \\textbf{\\scriptsize TOUCH} \& \\cellcolor{red} \\textbf{\\scriptsize \\textcolor{white}{NO TOUCH}} \\newline \\hline \\end{tabular} \\hspace{.9em}")
}
}
for(i in 1: nrow(touchNoTouchDF)) {
print(touchNoTouchDF[i,2])
}
```
What I did here, was make a look to generate the correct LaTex code in a different column of that data frame based on what the first column contains. After running that for loop I get the data frame you see above. So far, excellent. Now is where my problem comes in. I want to have that R code chunk print that LaTex so it is rendered. I figured I simple for loop with a print statement would work, no dice. I tried cat(), that didn't work either.
I'm perplexed as to how I can get the contents of that last row printed out so the pdf report that is generated contains that graphical representation. Any ideas? Tips? I'm sure this is simple, but I have been at this for couple hours and Google'd it various ways... Thanks in advance!
EDIT:
As requested, below is the output of what I am getting. I made sure to use cat(), which, if I run this manually, prints the LaTex out to the console with no issues. When I attempt to knit the document, I get the following:
! Misplaced \noalign.
\hline ->\noalign
{\ifnum 0=`}\fi \let \hskip \vskip \let \vrule \hrule \let...
l.143 ...tbf{\scriptsize NO TOUCH} \newline \hline
pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning messages:
1: package 'knitr' was built under R version 3.3.2
2: package 'prettyR' was built under R version 3.3.2
3: package 'magick' was built under R version 3.3.2
4: running command '"C:/Users/z10987/AppData/Local/Pandoc/pandoc" +RTS -K512m -RTS pdfReport.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pdfReport.pdf --template "C:\R\R-3.3.1\library\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine xelatex --include-in-header header1.tex --variable graphics=yes' had status 43
Execution halted
I thought, maybe, it was because I did not escape the "&" in the table, but when I tried that I got:
Error: \& is an unrecognized escape character in string...
Thanks again!
UPDATE:
I figured it out - in the table, originally, at the end of each line there was a \\
. I attempted to escape this by \\\
, which gave me an error. I decided that since I thought \\
was a newline, I'd just replace it with \\newline
. I guess you have to have \\
at the end of a table, or it doesn't like it.
So... I put the \\
back in. Now, but I still had the issue with escaping it. Just on a whim, I figured, well, "if one \ escapes the other \, maybe I need two, one to escape each backslash..." I replaced the \\
with \\\\
and voila! It worked. Ugh, such simple things.... I wanted to post the solution for this.
cat()
rather thanprint()
. Show us what that prints, and what errors or problems you get. – user2554330