【问题标题】:Italicising the headings of a dataframe in rmarkdown在 rmarkdown 中斜体数据框的标题
【发布时间】:2018-02-17 06:00:12
【问题描述】:

我想对 rmarkdown 中的 pander 表中的列标题应用一些乳胶样式的格式,编织成 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")
```

【问题讨论】:

    标签: r r-markdown pandoc pander


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      这是一个基于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 输出中:

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

      【讨论】:

      • 很高兴知道huxtable。我以前没遇到过。
      猜你喜欢
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 2021-06-24
      相关资源
      最近更新 更多