Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xtable adding a title on top and a caption under the table

I want to put a caption title on the xtable in a Rnw document. Here's the code. Unfortunately, I'm not able to add a caption under the table. I've tried the \caption{} function, but it won't print the PDF.

I've seen R: xtable caption (or comment), but it's not working for a table that was created from the lm() function in R. Do you have any clue?

<<yoman,echo=FALSE,results=tex>>=
library(xtable)

pop5lm <- lm(mpg ~ wt, data=mtcars) #my linear model

print(xtable(pop5lm,
             caption = c("Estimates of linear model for father Muro CB"), 
             label = "tab:one", digits = c(0,2, 2, 2,3)), 
             table.placement = "tbp", 
             caption.placement = "top")
@
like image 402
M. Beausoleil Avatar asked Nov 06 '14 22:11

M. Beausoleil


1 Answers

I couldn't see an quick option in xtable to add text to the bottom of the table (that doesn't mean there isn't one) so i have used an idea from here and from the link in your question. It is a rather crude fix with the large drawback that you need to specify the width of the text to add (equal to the width of the table) - if you make it too long it stretches the final column (to see change 8.5 to 10).

\documentclass{article}

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}

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

<<yoman,echo=FALSE,results=tex>>=
library(xtable)

mod <- lm(mpg ~ wt, data=mtcars) #my linear model

print(xtable(mod,
             caption = "Estimates of linear model for father Muro CB ", 
             #label = "tab:one", 
             digits = c(0,2, 2, 2,3)), 
             table.placement = "h!", 
             caption.placement = "top",
             add.to.row = list(list(2),  
             "\\hline  \\multicolumn{5}{L{8.5cm}}{\\textbf{Note: }
             This is a description, blah, blah, blah, blah,  blah, blah, 
             blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, 
             blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, 
             blah, blah, blah, blah, blah, blah} \\\\"))

@

\end{document}

enter image description here

I assume there are many alternatives in latex to accomplish this but might get you started.


From comments: I tried outputting it to html and didn't work. Any thoughts?

You can change the latex command multicolumn in the add.to.row argument of print.table to use html table functions instead. (using html output of Rmarkdown)

```{r,echo=FALSE, results='asis'}
library(xtable)

mod <- lm(mpg ~ wt, data=mtcars) #my linear model

print(xtable(mod,
             caption = "Estimates of linear model for father Muro CB ", 
             digits = c(0,2, 2, 2,3)), 
             type="html",
             caption.placement = "top",
             add.to.row = list(list(2),  
             '<tr><td colspan="5"><b>Note: </b>
             This is a description, blah, blah, blah, blah,  blah, blah, 
             blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, 
             blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, 
             blah, blah, blah, blah, blah, blah</td></tr>'))

```
like image 120
user20650 Avatar answered Oct 16 '22 14:10

user20650