在 rmarkdown 中斜体化数据框的标题

Italicising the headings of a dataframe in rmarkdown

我想对 rmarkdown 中的 pander table 中的列标题应用一些 latex-style 格式,编织成 pdf。

请注意,在下面的玩具文档中,适用于数据框元素的乳胶命令不适用于标题。具体来说,我希望 (1) 能够将某些标题设为斜体,(2) 能够在字母之间添加空格的标题(此时 R 会自动添加 .)。但是,我通常对如何获取数据框中的标题以接受与数据框元素相同的乳胶命令感兴趣。

 ---
title: "Chapter 12: Adding to the Discrete Time Hazard Model"
output:
  pdf_document: default
  html_document: null
  word_document: null
toc: yes
linestretch: 1.3
classoption: fleqn
header-includes: 
 - \setlength{\mathindent}{0pt}
 - \setlength\parindent{0pt}
 - \usepackage{amssymb}
---

```{r global_options, include=FALSE, echo = FALSE}
#this sets global knit options (i.e. options for the entire document. The following supresses any warnings from being include in the output and sets plot parameters. Note that setting dev to pdf allows us to set size of graphs easily
rm(list = ls())

knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/', 
                      echo=FALSE, warning=FALSE, message=FALSE, dev = 'pdf')
```

``` {r table p 446}
abC <- 0.3600344
bC <- 0.2455304 
intC <- 0.4787285


dfTrans <- data.frame("Prototype" = c("$\textit{Left/not Blue}$", "Left/Blue", "Right/not Blue", "Right/Blue"),
                      "$LEFT$" = c(0,1,0,1),
                      "$\textit{BLUE}$" = c(0,0,1,1),
                      `Combined Parameter Estimates` = c(paste("0 x ", round(abC,4), "+ 0 x", round(bC,4), "+ 0 x", round(intC, 4), sep = " "),  8, 9, 0))


library(pander)
panderOptions('table.split.table', 300) # this forces the table to the width of the page. 
pander(dfTrans, justify = "left")
```

我不确定如何使用 pander 执行此操作,但这里有一个使用 knitrkableExtra functions 中的 kable 函数的方法,用于详细 table 格式化。我没有更改 yaml 标记,但更新后的代码块粘贴在下面,然后是输出。

```{r global_options, include=FALSE, echo = FALSE}
#this sets global knit options (i.e. options for the entire document. The following supresses any warnings from being include in the output and sets plot parameters. Note that setting dev to pdf allows us to set size of graphs easily
knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/', 
                      echo=FALSE, warning=FALSE, message=FALSE, dev = 'pdf')

# rm(list = ls()) This is unnecessary. knitr runs the rmarkdown document in a clean session.

library(knitr)
library(kableExtra)
options(knitr.table.format = "latex")  # latex output (instead of default html)
library(tidyverse) # For dplyr pipe (%>%) and mutate
```

```{r table p 446}
abC <- 0.3600344
bC <- 0.2455304 
intC <- 0.4787285

# I've removed the latex formatting from the data frame code
dfTrans <- data.frame(Prototype = c("Italic_Left/not Blue", "Left/Blue", "Right/not Blue", "Right/Blue"),
                      LEFT = c(0,1,0,1),
                      BLUE = c(0,0,1,1),
                      `Combined Parameter Estimates` = c(paste("0 x ", round(abC,4), "+ 0 x", round(bC,4), "+ 0 x", round(intC, 4), sep = " "),  8, 9, 0))

# Remove periods in column names
names(dfTrans) = gsub("\.", " ", names(dfTrans))
# Two other options: 
# 1. Use the data_frame function from tidyverse, rather than the base data.frame function. 
#    data_frame doesn't add periods, so you won't need to fix the column names afterwards.
# 2. Set check.names=FALSE in data.frame

# Use kableExtra cell_spec function to format on a cell-by-cell basis
dfTrans = dfTrans %>% 
  mutate(Prototype = cell_spec(Prototype, color=c("black","blue"),
                               align=rep(c("l","r"), each=2)))

# Format each of the column names using kableExtra text_spec
names(dfTrans)[1] = text_spec(names(dfTrans)[1], italic=TRUE)
names(dfTrans)[2] = text_spec(names(dfTrans)[2], align="l")
names(dfTrans)[3] = text_spec(names(dfTrans)[3], align="r", italic=TRUE, color="blue")
names(dfTrans)[4] = text_spec(names(dfTrans)[4], align="r")

# Output the table
kable(dfTrans, booktabs=TRUE, escape=FALSE) 
```

我不确定该怎么做的一件事是仅将 dfTrans$Prototype 的第一个值格式化为斜体。 cell_spec 似乎只使用了 italic 逻辑向量的第一个值,所以下面的斜体整列:

dfTrans = dfTrans %>% 
  mutate(Prototype = cell_spec(Prototype, color=c("black","blue"),
                               align=rep(c("l","r"), each=2),
                               italic=c(TRUE, rep(FALSE, n()-1))))

这里有一个基于huxtable的解决方案(我的包):

abC  <- 0.3600344
bC   <- 0.2455304 
intC <- 0.4787285
dfTrans <- data.frame(Prototype = c("Italic_Left/not Blue", "Left/Blue", "Right/not Blue", "Right/Blue"),
                  LEFT = c(0,1,0,1),
                  BLUE = c(0,0,1,1),
                  `Combined Parameter Estimates` = c(paste("0 x ", round(abC,4), "+ 0 x", round(bC,4), "+ 0 x", round(intC, 4), sep = " "),  8, 9, 0))

library(huxtable)
huxTrans <- hux(dfTrans, add_colnames = TRUE) # column names become first row
huxTrans[1, 4] <- 'Combined Parameter Estimates' # get rid of the dots
align(huxTrans)[4:5, 1] <- 'right'
text_color(huxTrans)[c(3, 5), 1] <- 'blue'
text_color(huxTrans)[1, 3] <- 'blue'
italic(huxTrans)[1, c(1, 3)] <- TRUE

huxTrans # will automatically become LaTeX in RMarkdown
quick_pdf(huxTrans)

在终端中看起来像这样:

在 PDF 输出中:

如果需要,您也可以添加边框。