如何用表r包添加标题?

时间:2021-05-06 14:29:42

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

使用iris数据,我们使用表包创建一个表:

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:

使用Hmisc包,我们将添加带有latex功能的标题,如下所示:

latex(table, caption="My table")

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

但这不适用于tables包,因为它定义了一个S3方法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?

但它不起作用,它说标题在浮动之外。如何使用tables包中的latex.tabular函数正确添加标题?

1 个解决方案

#1


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.

在knitr的一个例子中,<< >> =是r块代码。

\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): 如何用表r包添加标题?

这产生了这个很棒的表,现在有了标题(用葡萄牙语):

#1


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.

在knitr的一个例子中,<< >> =是r块代码。

\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): 如何用表r包添加标题?

这产生了这个很棒的表,现在有了标题(用葡萄牙语):