3

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?

2 Answers 2

2

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

0

Is there anything that speaks against making a proper data.frame with R and use the xtable library to have a nice latex output? Here is an example. Of course you will have to change the rownames() according to what you want.

\documentclass{article}

<<R library>>=
library(xtable)
@


\begin{document}

<<R dataframe , results = "asis" >>=
df <- data.frame ("Observations" = c(a = "1%", b = "2%", c = "3%", d = "4%"),
                  "Event rate" = c(1:4)
)

xtable(df)
@


\end{document}
2
  • I did not know about xtable. Thank you. However, when I post the code you posted to a fresh .rnw-file, I get "Running pdflatex.exe on test2.tex...failed" again. LOG: Overfull \hbox (59.24648pt too wide) in paragraph at lines 65--65 [][]\OT1/cmtt/m/n/10 df[] []<-[] []data.frame[] []([][]"Observations"[] []=[] [ ]c[][]([][]a[] []=[] []"1%"[][],[] []b[] []=[] []"2%"[][],[] []c[] []=[] []"3%" [][],[] []d[] []=[] []"4%"[][]),[][] [] Any tips? May 9, 2016 at 21:37
  • @FrederikVittingHermann A Problem with Latex ...Can you give some infos about your OS (Windows I guess...). Are you up-to-date with LaTex? Did you check the Config in RStudio (Tools/Global Options/Sweave) Try with knitr and XeLatex. And very important : check the encoding of your file UTF-8. If nothing helps, could you post test2.tex?
    – nebi
    May 10, 2016 at 10:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .