group_rows()命令中的换行时缩进-R markdown中的kableExtra包 [英] Indentation when line break in group_rows() command - kableExtra package in R markdown

查看:86
本文介绍了group_rows()命令中的换行时缩进-R markdown中的kableExtra包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用kableExtra包以R降价将表格输出为PDF.

I'm using the kableExtra package to output a table to PDF in R markdown.

我使用命令group_rows()将表的某些行分组在一起.

I use the command group_rows() to group some rows of my table together.

第一列某些行中的文本对于列宽而言太长,因此分成两行.但是,第二行没有缩进.是否可以缩进第二行或整体删除缩进?

The text in some rows of my first column is too long for the column width, so it is broken into two lines. However, there is no indentation of the second line. Is there a way to either indent also the second line or remove the indentation overall?

不幸的是,增加列宽以使文本不会分布在两行上是不幸的选择,因为我的真实表中有更多列.

Increasing the column width so the text won't be spread over two lines is unfortunately no option since I have way more columns in my real table.

这是我数据框的子集:

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")

这是我正在使用的代码:

This is the code I am using:

```{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输出是这样的.如您所见,例如文本最高所得税率"分为两行,我希望第二行像第一行一样缩进.

This is how the .pdf output looks like. As you can see, e.g. the text "top income tax rate" is split into two lines and I would like the second line to be indented just like the first line.

谢谢您的提示!

推荐答案

如果仅在R控制台中运行块,您将看到以下LaTeX输出:

If you just run the chunk in the R console, you'll see this LaTeX output:

\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环境中,并摆弄空格以使其看起来更好.

As you can see, kableExtra isn't putting in a line break in that title, LaTeX is doing it. This means you need a LaTeX fix for the problem. Maybe someone else knows an easier one, but the best I could find is the following: wrap the long row title in a minipage environment, and fiddle with the spacing to look better.

由于这有点凌乱,所以我要编写一个R函数来做到这一点:

Since this is kind of messy, I'd write an R function to do it:

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软件包.因此,总体而言,您的示例文档将如下所示:

This needs to be called on the data being put into the table, and kable needs to be told not to escape those backslashes (using escape = FALSE). In addition, the \setstretch command comes from the setspace LaTeX package. So overall your sample document would look like this:

---
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")
```

有了该代码,我看到了:

With that code I see this:

间距不是很正确,但是越来越近了.我希望这会有所帮助.

The spacing isn't quite right, but it's getting close. I hope this helps.

这篇关于group_rows()命令中的换行时缩进-R markdown中的kableExtra包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆