kableExtra: short.caption argument does not work if several dataframes are included in one table

孤者浪人 提交于 2021-01-27 07:43:32

问题


I have recently noticed that kable() from the kableExtra-package behaves oddly if more than one dataframe is included in the command.

Short.caption argument is not used

In this example, I create two data-frames and want to include them into one table.

library(kableExtra)
t1 <- c(68, 48, 50, 113, 98, 94)
t2 <- c(26, 16, 22, 30, 16, 12)
Group <- c("b1", "b2", "b3", "b4", "b5", "b6")
cows <- data.frame(Group, t1, t2)
colnames(cows) <- c("Group", "t1", "t2")

kable(list(cows[1:3,], cows[4:6,]), 
     format = "latex",
     caption= "Number of some variables used in some groups",
     caption.short = "Anything shorter.",
     booktabs = T, row.names = F) %>%
  kable_styling(latex_options = c("striped", "hold_position"))

Assuming \listoftables is included at the beginning of the Rmarkdown-file, it will not recognize the short caption. If I look at the actual Latex-code, the [Anything shorter.] is not included:

\begin{table}[!h]
\caption{\label{tab:}Number of some variables used in some groups}

\centering
\begin{tabular}[t]{lrr}
\toprule
Group & t1 & \vphantom{1} t2\\
\midrule
\rowcolor{gray!6}  b1 & 68 & 26\\
b2 & 48 & 16\\
\rowcolor{gray!6}  b3 & 50 & 22\\
\bottomrule
\end{tabular}
\centering
\begin{tabular}[t]{lrr}
\toprule
Group & t1 & t2\\
\midrule
\rowcolor{gray!6}  b4 & 113 & 30\\
b5 & 98 & 16\\
\rowcolor{gray!6}  b6 & 94 & 12\\
\bottomrule
\end{tabular}
\end{table}

If I exclude one table, the [Anything shorter.] argument appears:

library(kableExtra)
t1 <- c(68, 48, 50, 113, 98, 94)
t2 <- c(26, 16, 22, 30, 16, 12)
Group <- c("b1", "b2", "b3", "b4", "b5", "b6")
cows <- data.frame(Group, t1, t2)
colnames(cows) <- c("Group", "t1", "t2")

kable(cows[4:6,], 
      format = "latex",
      caption= "Number of some variables used in some groups",
      caption.short = "Anything shorter.",
      booktabs = T, row.names = F) %>%
   kable_styling(latex_options = c("striped", "hold_position"))

The Latex code:

\begin{table}[!h]

\caption[Anything shorter.]{\label{tab:}Number of some variables used in some groups}
\centering
\begin{tabular}[t]{lrr}
\toprule
Group & t1 & t2\\
\midrule
\rowcolor{gray!6}  b4 & 113 & 30\\
b5 & 98 & 16\\
\rowcolor{gray!6}  b6 & 94 & 12\\
\bottomrule
\end{tabular}
\end{table}

I have experienced similar behaviour in the row_spec() and add_header_above() features. Up until now, I have manually inserted the relevant Latex-code to suit my work, but I was wondering whether others have had similar issues.

I am wondering whether this issue stems from an error on my side or is a potential issue in the kableExtra package.

来源:https://stackoverflow.com/questions/64281612/kableextra-short-caption-argument-does-not-work-if-several-dataframes-are-inclu

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