在rmarkdown中对数据框的标题进行斜体显示

时间:2018-02-17 06:00:12

标签: r r-markdown pandoc pander

我想将一些乳胶风格的格式应用于rmarkdown中的平移表中的列标题,编织为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")
```

2 个答案:

答案 0 :(得分:3)

我不确定如何使用pander执行此操作,但这是使用kablekableExtra functions中的knitr函数进行详细表格格式化的方法。我还没有更改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) 
```

enter image description here

我不确定如何做的一件事就是将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))))

答案 1 :(得分:3)

这是一个基于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)

在终端中看起来像这样:

RStudio terminal

这在PDF输出中:

PDF

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