group_rows()命令换行时缩进 - R markdown中的kableExtra包

时间:2018-06-14 13:57:29

标签: r rstudio r-markdown knitr kableextra

我使用kableExtra包在R markdown中将表格输出到PDF。

我使用命令group_rows()将表格的某些行组合在一起。

我的第一列的某些行中的文本对于列宽而言太长,因此它被分成两行。但是,第二行没有缩进。有没有办法缩进第二行或整体删除缩进?

增加列宽以使文本不会分布在两行上,遗憾的是没有选项,因为我在实际表格中有更多列。

这是我的数据框的一个子集:

data <- structure(list(`Control variables` = c("GDP growth", "GDP per capita", 
"Top income tax rate", "Right-wing executive"), Treated = structure(c("2.29", 
"21,523.57", "0.70", "0.62"), class = "AsIs"), top10_synthetic = structure(c("3.37", "19,939.72", "0.68", "0.63"), class = "AsIs"), top10_mean = structure(c("2.95", "30,242.60", "0.64", "0.43"), class = "AsIs")), .Names = c("Control variables", "Treated", "top10_synthetic", "top10_mean"), row.names = c(NA, 4L), class = "data.frame")

这是我正在使用的代码:

```{r}

kable(data, "latex", caption = "table 1", booktabs = T, col.names = c("Control variables", "Treated", "Synthetic", "Mean")) %>%
  add_header_above(c("", "", "Top 10%" = 2)) %>%
  group_rows("UK", 1, 2) %>%
  group_rows("Japan", 3, 4, latex_gap_space = "0.8cm") %>%
  footnote(general = "xxx") %>%
  kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
  column_spec(1, width = "3cm")

```

这就是.pdf输出的样子。如你所见,例如文本&#34;最高所得税税率&#34;被分成两行,我希望第二行像第一行一样缩进。

enter image description here

感谢您的任何提示!

1 个答案:

答案 0 :(得分:1)

如果您只是在R控制台中运行块,您将看到此LaTeX输出:

\begin{table}[H]

\caption{\label{tab:}table 1}
\centering
\resizebox{\linewidth}{!}{
\begin{tabular}[t]{>{\raggedright\arraybackslash}p{3cm}lll}
\toprule
\multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{2}{c}{Top 10\%} \\
\cmidrule(l{2pt}r{2pt}){3-4}
Control variables & Treated & Synthetic & Mean\\
\midrule
\addlinespace[0.3em]
\multicolumn{4}{l}{\textbf{UK}}\\
\hspace{1em}GDP growth & 2.29 & 3.37 & 2.95\\
\hspace{1em}GDP per capita & 21,523.57 & 19,939.72 & 30,242.60\\
\addlinespace[0.8cm]
\multicolumn{4}{l}{\textbf{Japan}}\\
\hspace{1em}Top income tax rate & 0.70 & 0.68 & 0.64\\
\hspace{1em}Right-wing executive & 0.62 & 0.63 & 0.43\\
\bottomrule
\multicolumn{4}{l}{\textit{Note: }}\\
\multicolumn{4}{l}{xxx}\\
\end{tabular}}
\end{table}

正如您所看到的,kableExtra并没有在该标题中添加换行符,LaTeX正在这样做。这意味着您需要针对该问题进行LaTeX修复。也许其他人知道一个更容易的,但我能找到的最好的是:在minipage环境中包裹长行标题,并调整间距以使其看起来更好。

由于这有点混乱,我会编写一个R函数来执行此操作:

inMinipage <- function(x, width) 
  paste0("\\begin{minipage}[t]{", 
         width, 
         "}\\raggedright\\setstretch{0.8}", 
         x, 
         "\\vspace{1.2ex}\\end{minipage}")

这需要在放入表中的数据上调用,并且需要告诉kable不要转义那些反斜杠(使用escape = FALSE)。此外,\setstretch命令来自setspace LaTeX包。总的来说,您的示例文档将如下所示:

---
output: 
  pdf_document:
    extra_dependencies: setspace
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
library(knitr)
```

```{r}
inMinipage <- function(x, width) 
  paste0("\\begin{minipage}[t]{", 
         width, 
         "}\\raggedright\\setstretch{0.8}", 
         x, 
         "\\end{minipage}")

data <- structure(list(`Control variables` = c("GDP growth", "GDP per capita", "Top income tax rate", "Right-wing executive"), Treated = structure(c("2.29", 
"21,523.57", "0.70", "0.62"), class = "AsIs"), top10_synthetic = structure(c("3.37", "19,939.72", "0.68", "0.63"), class = "AsIs"), top10_mean = structure(c("2.95", "30,242.60", "0.64", "0.43"), class = "AsIs")), .Names = c("Control variables", "Treated", "top10_synthetic", "top10_mean"), row.names = c(NA, 4L), class = "data.frame")

data[[1]] <- inMinipage(data[[1]], "2.5cm")

kable(data, "latex", caption = "table 1", booktabs = T, col.names = c("Control variables", "Treated", "Synthetic", "Mean"), escape = FALSE) %>%
  add_header_above(c("", "", "Top 10%" = 2)) %>%
  group_rows("UK", 1, 2) %>%
  group_rows("Japan", 3, 4, latex_gap_space = "0.8cm") %>%
  footnote(general = "xxx") %>%
  kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
  column_spec(1, width = "3cm")
```

使用该代码我看到了:

enter image description here

间距不太合适,但它越来越近了。我希望这会有所帮助。