[Tex/LaTex] Tables with reference to R objects in sweave / latex

knitrrrstudiosweavetables

I'm using knitr/R-sweave to produce a pdf-document wit LaTeX-code. I want to make a table with some numbers produced by some R calculations in the R-code-chuck above this table. When I usually want to refer to an R object in the LaTeX/knitr-outout I use \Sexpr{}, but it doesn't seem to work within tables.

Here's what I got:

\documentclass{article}

\begin{document}
<<Chunk1>>=
a= "1%"
b= "2%"
c= "3%"
d= "4%"
e= 1
f= 2
g= 3
h= 4
@

\begin{table}[]
\centering
\caption{Simple stratified data splits}
\label{my-label}
\begin{tabular}{lll}
\textbf{Dataset} & \textbf{Observations}     & \textbf{Event rate}\\ \hline
Full data        & \Sexpr{e}                & \Sexpr{a}         \\
Training set     & \Sexpr{f}                & \Sexpr{b}         \\
Test set         & \Sexpr{g}                & \Sexpr{c}         \\
Evaluation set   & \Sexpr{h}                & \Sexpr{d}        
\end{tabular}
\end{table}

\end{document}

For some reason when I only add the first two rows:

\textbf{Dataset} & \textbf{Observations}     & \textbf{Event rate}\\ \hline
Full data        & \Sexpr{e}                & \Sexpr{a}         \\

…it works fine. I have tried adding the same row four times but that still produces the error: "Running pdflatex.exe on test.tex…failed"

I get the following from the log:

LaTeX Warning: No positions in optional float specifier. Default added
(so using `tbp') on input line 71.

LaTeX Font Info: External font cmex10' loaded for size (Font)

<7> on input line 75. LaTeX Font Info: External fontcmex10'
loaded for size (Font) <5> on input line 75. [1{C:/Users/Frederik
Hermann/AppData/Local/MiKTeX/2.9/pdftex/config/pdftex.map}

] (test.aux) ) Here is how much of TeX's memory you used: 1244 strings
out of 493634 16308 string characters out of 3143709 72572 words of
memory out of 3000000 4665 multiletter control sequences out of
15000+200000 4116 words of font info for 16 fonts, out of 3000000 for
9000 1025 hyphenation exceptions out of 8191 27i,8n,19p,284b,237s
stack positions out of 5000i,500n,10000p,200000b,50000s Output written
on test.pdf (1 page, 39879 bytes). PDF statistics: 18 PDF objects out
of 1000 (max. 8388607) 0 named destinations out of 1000 (max. 500000)
1 words of extra memory for PDF output out of 10000 (max. 10000000)

I'm quite the rookie. Any ideas?

Best Answer

Your problem comes from using the "%" symbol in the RChunk. When compiled by either knitr or sweave the "%" is passed through to LaTeX and then interpreted as the beginning of a comment, there by causing the rest of that line in the TeX file to be ignored. Here is the revised code followed by the output. (Note: I also turned off the echo of the R code in the output file. This compiles fine with pdflatex on Windows 10.

\documentclass{article}

\begin{document}
<<Chunk1,echo=FALSE>>=
a= "1 "
b= "2 "
c= "3 "
d= "4 "
e= 1
f= 2
g= 3
h= 4
@

\begin{table}[]
\centering
\caption{Simple stratified data splits}
\label{my-label}
\begin{tabular}{lll}
\textbf{Dataset} & \textbf{Observations}     & \textbf{Event rate}\\ \hline
Full data        & \Sexpr{e}                & \Sexpr{a}         \\
Training set     & \Sexpr{f}                & \Sexpr{b}         \\
Test set         & \Sexpr{g}                & \Sexpr{c}         \\
Evaluation set   & \Sexpr{h}                & \Sexpr{d}        
\end{tabular}
\end{table}

\end{document} 

enter image description here