【问题标题】:Using R-markdown knitr hooks to custom format tables in HTML reports使用 R-markdown knitr hooks 自定义 HTML 报告中的表格格式
【发布时间】:2018-09-25 05:50:48
【问题描述】:

我正在尝试设置一个knitr::knit_hooks(),以便在我的 HTML 报告中使用kableExtra 自动格式化 R-markdown 块的数据框输出。

我不想在每个列表数据块的末尾重复添加以下行(或任何行):

head(iris) %>%
  kable("html") %>%
  kable_styling("hover", full_width = FALSE)

我想出了一个基于this answer 的解决方案,它通过评估块source 来工作(请参阅我的answer below,其中包括我在使用这种方法时遇到的一些问题);我希望使用块 output 可能会有更好的解决方案。

这是一个示例 .Rmd,其中概述了我想要实现的目标。

---
title: "Untitled"
author: "Paul"
date: "25 September 2018"
output: html_document
---

```{r setup, include = F}

library(dplyr)
library(kableExtra)
library(knitr)

data(iris)

default_source_hook <- knit_hooks$get('source')

knit_hooks$set(
  output = function(x, options) {
    x %>%
      kable("html") %>%
      kable_styling("hover", full_width = FALSE)
  },
  source = function(x, options) {
    if(is.null(options$table))
      default_source_hook(x, options)
    else {
      eval(parse(text = x)) %>%
        kable("html") %>%
        kable_styling("hover", full_width = F)
    }}
)


```

Desired chunk input:

```{r test, echo = F}
head(iris)

```

Desired output will look like:

```{r output, echo = F}
head(iris) %>%
  kable("html") %>%
  kable_styling("hover", full_width = FALSE)

```

Solution using the source chunk output:

```{r table_format, results = "hide", table = T, eval = F}
head(iris)

```

谢谢。

【问题讨论】:

    标签: r hook r-markdown knitr


    【解决方案1】:

    如果不需要使用针织钩,以下可能会有所帮助。这个想法是只定义一个函数,它可以按照你想要的方式打印它得到的任何东西。这并没有消除所有的打字,而是大大减少了打字。

    ---
    title: "Untitled"
    author: "Paul"
    date: "25 September 2018"
    output: html_document
    ---
    
    ```{r setup, include = F}
    
    library(dplyr)
    library(kableExtra)
    library(knitr)
    
    tbl_out <- function(data) {
      data %>% kable("html") %>% kable_styling("hover", full_width = FALSE)
    }
    ```
    
    Prints as desired:
    
    ```{r test, echo = F}
    head(iris) %>% tbl_out()
    ```
    

    输出:

    【讨论】:

      【解决方案2】:

      我通过使用knit_hooks() source 作为格式化的块输出找到了解决方法。

      格式化块输出的难点在于字符数据传递给钩子。从 OP 中的链接答案中汲取灵感,我通过评估 knit_hooks() 中的块 source 找到了解决方案。

      此解决方案重新评估块并通过管道传输到所需的KableExtra 格式。

      我需要添加一个新的块选项table = T,以便格式化只应用于生成数据表的源代码;还需要results = "hide",因此报告中不包含默认块输出。

      ---
      title: "Untitled"
      author: "Paul"
      date: "27 September 2018"
      output: html_document
      ---
      
      ```{r setup, include = F}
      
      library(dplyr)
      library(ggplot2)
      library(kableExtra)
      library(knitr)
      
      default_source_hook <- knit_hooks$get('source')
      
      knit_hooks$set(
        source = function(x, options) {
          if(is.null(options$table))
            default_source_hook(x, options)
          else {
            eval(parse(text = x)) %>%
              kable("html") %>%
              kable_styling("hover", full_width = F)
          }
      
        }
      )
      
      data(iris)
      
      ```
      
      ## Normal
      With no chunk options:
      
      ```{r normal}
      head(iris)
      
      ```
      
      ## The desired ouptut
      With chunk options `results = "hide"` and `table = T`:
      
      ```{r table_format, results = "hide", table = T, eval = F}
      head(iris)
      
      ```
      
      ## It still work as normal for other other output types
      With no chunk options:
      
      ```{r image}
      iris %>%
        ggplot(aes(x = Sepal.Length, y = Sepal.Width, group = Species)) +
        geom_point()
      
      ```
      

      此解决方案存在一些问题

      1. 用于生成表格的代码现在不可用
      2. eval = F 应该包括在内 - 特别是如果代码需要一段时间才能运行(因为它再次在钩子中进行评估)
      3. 我将 2 行代码替换为 3 个块选项 - 虽然不是交易杀手,但它并不像我希望的那样自动。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-19
        • 1970-01-01
        • 1970-01-01
        • 2016-01-27
        • 2020-05-20
        • 1970-01-01
        • 1970-01-01
        • 2014-12-13
        相关资源
        最近更新 更多