R / Sweave / Latex - Post a comment in a table (xxtable)

I created a table using R and sweave in LaTeX. Sweave example:

\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}

<<label=tab1, echo=FALSE, results=tex>>=
library(xtable)
employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800)
mData <- data.frame(employee, salary) 
print(xtable(mData, caption = "Salary", align="ccc"), caption.placement="top", hline.after = c(c(-1, 0), nrow(mData)), include.rownames=FALSE) 
@

\end{document}

The basic structure of LaTeX tables

\begin{table}
\begin{tabular}{cc}
...
\end{tabular}
\end{table}

To save a lot of work, I use the print and xtable functions in R to create the table code in LaTeX. But now I want to add text between the \ end {tabular} and \ end {table} statements. The add.to.row argument does not help with the print function, because the instructions only fit before \ end {tabular}. How can I solve this problem?

Many thanks for your help.

+2
source share
1 answer

The way to do this is to use xtable, remove the table environment float = FALSE and the threeparttable package

\documentclass{article}
\usepackage[para,online,flushleft]{threeparttable}
\begin{document}
\SweaveOpts{concordance=TRUE}

<<label=tab1, echo=FALSE, results=tex>>=
library(xtable)
employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800)
mData <- data.frame(employee, salary) 
options(xtable.comment = FALSE)
xt<-xtable(mData, caption = "Salary", align="ccc") 
print(xt,floating = FALSE,
    caption.placement="top",
    hline.after = c(c(-1, 0), nrow(mData)),
    include.rownames=FALSE,
    file="test.tex"
    )
@


\begin{table}[h]
\caption{A table with notes in the end}
  \begin{center}
     \begin{threeparttable}
       % INPUT YOUR TEX HERE :
       \input{test.tex}
     \begin{tablenotes}
       \item[1] aaaa; \item[2] bbbb
     \end{tablenotes}
    \end{threeparttable}
   \end{center}
 \label{table:tablewithnotes}
 \end{table}     
\end{document}`enter code here

Table output

0
source

Source: https://habr.com/ru/post/1688225/


All Articles