[Tex/LaTex] Embedding R (kable) results in LaTeX

knitrrrnw

Apology at first if this question has been asked or the sample is not reproducible. I'm totally new to LaTex and happy to take any advice.

I am working on my thesis on a .tex LaTex template given by the university, and I just saved it as a .rnw file since I would like to include some R results. Before I added code, the file can knitr well and generate a pdf.

\documentclass[master,tocprelim]{cornell}
\usepackage{graphicx,pstricks}
\usepackage{graphics}
\usepackage{moreverb}
\usepackage{subfigure}
\usepackage{epsfig}
\usepackage{hangcaption}
\usepackage{txfonts}
\usepackage{palatino}
\graphicspath{ {figures/} }

\begin{document}

He said it is cool\cite{tkh1998}. 

<<code, echo=FALSE, message=FALSE, warning=FALSE>>=
library(kableExtra)
library(tidyverse)
library(dplyr)
data = data.frame(matrix(rnorm(20, 10, 2), ncol=4))
kable(data, 'latex', caption = 'Time series Errors', 
        longtable = T, booktabs = T) %>%
  kable_styling(latex_options = c("striped", "hold_position", "repeat_header"))
@

\appendix

%\bibliography{LR}

\end{document}

(I don't know how to include reference in the .rnw file, but my .bib file include this citation:

@ARTICLE{tkh1998, 
author={ {Tin Kam Ho}}, 
journal={IEEE Transactions on Pattern Analysis and Machine Intelligence}, 
title={The random subspace method for constructing decision forests}, 
year={1998}, 
volume={20}, 
number={8}, 
pages={832-844}, 
keywords={pattern classification;trees (mathematics);decision theory;random processes;learning (artificial intelligence);random subspace method;decision forests;decision trees;overfitting;maximum accuracy;decision tree based classifier;generalization accuracy;feature vector;classification accuracy;Decision trees;Classification tree analysis;Training data;Clustering algorithms;Tin;Stochastic systems;Binary trees;Support vector machines;Support vector machine classification}, 
doi={10.1109/34.709601}, 
ISSN={0162-8828}, 
month={Aug},}

However, after I added:

<<code, echo=FALSE, message=FALSE, warning=FALSE>>=
library(kableExtra)
library(tidyverse)
library(dplyr)
data = data.frame(matrix(rnorm(20, 10, 2), ncol=4))
kable(data, 'latex', caption = 'Time series Errors', 
        longtable = T, booktabs = T) %>%
  kable_styling(latex_options = c("striped", "hold_position", "repeat_header"))
@

This file cannot be knitr anymore. It produced errors including "citation tkh1998 undefined on input line 86" (I don't even have line 86!!), "Incompatible color definition on input line 89", "Undefined control sequence".

I appreciate any suggestion about integrating LaTeX output and R results together (especially KableExtra outputs), shall I mainly write in .tex or .rnw, etc.

Best Answer

Welcome to TeX.SE. This answer suggests that you use the biblatex package and biber to produce your citations and bibliography. The answer then assumes you are using RStudio as your editor and to compile your .Rnw file.

Executing biber from within RStudio was answered here: How to insert references and bibliography into a .Rnw file produce with RStudio R Sweave and knitr?

You need to add this code chunk:

<<biber, eval= TRUE, include= FALSE, cache= FALSE, echo= FALSE>>=
system (paste ("biber", sub ("\\.Rnw$", "", current_input())))
@

This will call biber to produce the .bbl file required for producing a bibliography. Note that RStudio automatically deletes this file at the end of the compilation so you cannot see it.

Compile the .Rnw file twice to produce the citations and bibliography.

I removed packages not required for this MWE. However, since you have the kable option booktabs = T you will need to load the booktabs package with \usepackage{booktabs}. Another kable option used in the MWE is striped. This uses \rowcolor from the xcolor package, so you also need to load that as well. Finally, the table is not being set using the longtable environment, so write longtable = F. You need longtable = T when the table will span more than one page.

This is output:

enter image description here

This is the MWE;

\begin{filecontents}{\jobname.bib}
@ARTICLE{tkh1998,
author={ {Tin Kam Ho}},
journal={IEEE Transactions on Pattern Analysis and Machine Intelligence},
title={The random subspace method for constructing decision forests},
year={1998},
volume={20},
number={8},
pages={832-844},
keywords={pattern classification;trees (mathematics);decision theory;random processes;learning (artificial intelligence);random subspace method;decision forests;decision trees;overfitting;maximum accuracy;decision tree based classifier;generalization accuracy;feature vector;classification accuracy;Decision trees;Classification tree analysis;Training data;Clustering algorithms;Tin;Stochastic systems;Binary trees;Support vector machines;Support vector machine classification},
doi={10.1109/34.709601},
ISSN={0162-8828},
month={Aug},}
}
\end{filecontents}

\documentclass{article}
\usepackage{booktabs}         % reqired for booktabs=T
\usepackage[backend= biber]{biblatex}
\usepackage[table]{xcolor}    % required for "striped"
\addbibresource{\jobname.bib}

\begin{document}

He said it is cool\cite{tkh1998}.

<<code, echo=FALSE, message=FALSE, warning=FALSE>>=
library(kableExtra)
library(tidyverse)
library(dplyr)
data = data.frame(matrix(rnorm(20, 10, 2), ncol=4))
kable(data, 'latex', caption = 'Time series Errors',
        longtable = F, booktabs = T) %>%
  kable_styling(latex_options = c("striped", "hold_position", "repeat_header"))
@

\appendix

<<biber, eval= TRUE, include= FALSE, cache= FALSE, echo= FALSE>>=
system (paste ("biber", sub ("\\.Rnw$", "", current_input())))
@

\printbibliography

\end{document}