How to add caption with the tables r package?

落花浮王杯 提交于 2019-12-11 03:19:35

问题


Using the iris data, we make a table with the tables package:

library(tables)
table <- tabular( (Sepal.Length+Sepal.Width) ~
           Format(format(digits=2))*(mean + sd), data=iris )

With the Hmisc package, we would add the caption with the latex function like this:

latex(table, caption="My table")

But that does not work with the tables package, because it defines an S3 method, latex.tabular.

So I have tried this, following an example in the package vignette:

latex(table, options=list(toprule="\\caption{My table}"))

But it does not work, it says that caption is outside float. How can I correctly add caption with the latex.tabular function from the tables package?


回答1:


I figured out one way to do it and it is actually pretty easy: you just have to embed the tables package result inside the latex code.

Below one example with knitr, the << >>= is the r chunk code.

\begin{table}
\caption{My awesome table from tables package}
\begin{center}

<<cool multilevel table, results='asis', echo=FALSE>>=
latex(table)
@

\end{center}
\label{tab:mytable}
\end{table}

This generated this awesome table, now with title (in portuguese):




回答2:


I am posting this to help late to help other people looking for help with this. I have been working to understand the tables package recently. The workflow I use is to export the table to a .tex file then pull those into my document which I compile with TexStudio. The solution above wont work with this workflow, so I am providing one which will, it will produce a caption below the table:


latex(table, , options=list(bottomrule = "\\bottomrule\\\\
                                             \\caption{My table}")

This will produce one above:


latex(table, , options=list(bottomrule = "\\caption{My table}\\\\
                                             \\toprule")



来源:https://stackoverflow.com/questions/21541718/how-to-add-caption-with-the-tables-r-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!