【发布时间】:2019-04-02 16:36:56
【问题描述】:
我正在尝试为 R 中“tables”包的表格输出添加标题,然后使用 kableExtra 进行其他格式化。
虽然其他线程已经找到了通过使用额外的 LaTeX 代码(Caption not appearing for LaTeX table when knitting using Hmisc LaTeX Function 和 Hmisc::latex not printing caption w/ tabular object)来添加标题标题的方法,但这些解决方案似乎与更新的 toKable() 函数不兼容,该函数允许使用 kableExtra 进行额外的格式化。
通常会在使用kable(x, caption = "mycaption") 时添加标题。但是,当它如下生成时,会发生错误(Error in toKable(., booktabs = T) : 'table' must be a 'tabular' object.)。如果我尝试通过latex() 添加任何其他格式,例如添加标题,则对象类型将发生变化,使其无法与toKable() 函数一起使用。任何有关如何使用 toKable() 以及已通过 latex() 的其他 LaTeX 格式的见解将不胜感激!
library(tables)
library(magrittr)
library(kableExtra)
tabular((Species + 1) ~ (n=1) + Format(digits=2)* (Sepal.Length + Sepal.Width)*(mean + sd), data=iris) %>%
latex(., options = list(tabular = "longtable",
toprule = "\\caption{Table 1. My favorite caption}\\\\\\toprule")) %>%
toKable(., booktabs = T)
传递给toKable()之前的LaTeX输出:
\begin{longtable}{lccccc}
\caption{Table 1. My favorite caption}\\\toprule
& & \multicolumn{2}{c}{Sepal.Length} & \multicolumn{2}{c}{Sepal.Width} \\
Species & n & mean & sd & mean & \multicolumn{1}{c}{sd} \\
\hline
setosa & $\phantom{0}50$ & $5.01$ & $0.35$ & $3.43$ & $0.38$ \\
versicolor & $\phantom{0}50$ & $5.94$ & $0.52$ & $2.77$ & $0.31$ \\
virginica & $\phantom{0}50$ & $6.59$ & $0.64$ & $2.97$ & $0.32$ \\
All & $150$ & $5.84$ & $0.83$ & $3.06$ & $0.44$ \\
\hline
\end{longtable}
【问题讨论】:
标签: r formatting caption kableextra