【问题标题】:HTML code inside of a R-markdown block for a single line单行的 R-markdown 块内的 HTML 代码
【发布时间】:2016-02-02 09:51:05
【问题描述】:

我在 for 循环中有一个 R-markdown 文档(测试各种模型),我想用 HTML 标头设置它们,否则很难找到我正在寻找的模型。有"asis" 选项,但这会关闭整个块的格式,这不是我想要的。我尝试了一些我在这里找到的东西,但没有任何效果。这是我的代码:

---
title: "Untitled"
author: "Mike Wise - 25 Jul 2014"
date: "November 2, 2015"
output: html_document
---

Test

```{r, echo=T}
for (i in 1:3){
  print("<h1>Title</h1>")
  #print("##Title")
  m <- data.frame(matrix(runif(25),5,5))
  print(m)
}
```

这是一个没有正确标题格式的尝试:

下面是 results="asis" 选项的样子:

【问题讨论】:

  • 为什么不使用 kable(m, format = "html") 并保留 asis?
  • 对表格没问题,我考虑过,但在这个应用程序中,打印出来的东西是不同算法的收敛消息,即不是表格,对于 for 循环的每次迭代也不相同.所以这不是一个真正的选择。\
  • 如果您有来自模型拟合结果的收敛消息,您可能需要查看 broom::tidy 以将您的模型拟合结果转换为 data.frames,然后再次使用 kable 显示。跨度>
  • 嗯,有很多东西,真的。收敛消息,摘要,混淆矩阵,我可能会发现对故障排除和/或在算法之间进行选择有用的各种东西。我有点想保持简单。但如果没有办法按照我的要求去做,那么我想我将不得不走那条路。
  • 如果它不是数据框输出,您可以包含您拥有的输出示例。

标签: html r regex knitr r-markdown


【解决方案1】:

好吧,一年半过去了,我仍然不时需要这个,但从那时起我学到了足够多的知识,知道如何正确地做到这一点。您需要编写一个knitr“输出挂钩”,并修改输出,以便 html 可以“逃脱”。

以下实现:

  • 添加了knitr 输出挂钩。
  • 定义了一个语法来指定所需的标签和内容
    • 例如获取&lt;h1&gt;some_text&lt;/h1&gt; 使用htmlesc&lt;&lt;(h1,some_text)&gt;&gt;
  • 想出一个正则表达式来提取h1some_text 并将其重新格式化为正确标记的html,同时删除knitr 插入的##
  • 添加了更多测试用例以确保它执行了一些额外的操作(例如h4p、正确放置图表和表格等)
  • 添加了另一个正则表达式来删除双转义行,这会添加一些不需要的空格面板结构。

代码如下:

---
title: "Output Hook for HTML Escape"
author: "Someone"
date: "2017 M04 25"
output: 
  html_document:
    keep_md: true
---

```{r setup, include=T,echo=TRUE}
knitr::opts_chunk$set(echo = TRUE)
hook_output <- knitr::knit_hooks$get("output")


knitr::knit_hooks$set(output=function(x,options){
  xn <- hook_output(x,options)
  # interestingly xn is a big character string. 
  # I had expected a list or vector, but the length of x is 1 and the class is character.

  # The following regexp extracts the parameters from a statement of the form
  # htmlesc<<(pat1,pat2)>> and converts it to <pat1>pat2</pat1>
  xn <- gsub("## htmlesc<<([^,]*),{1}([^>>]*)>>","\n```\n<\\1>\\2</\\1>\n```\n",xn)
  # now remove double escaped lines that occur when we do these right after each other
  gsub(">\n```\n\n\n```\n<",">\n<",xn)
}
)
```

## An analysis loop in a single R chunk with R Markdown

In the following, we do a loop and generate 3 sets of data:

(@) We have some explanitory text
(@) then we do a bar plot with ggplot
(@) then we print out a table
(@) then we do a base plot - just for fun

```{r, echo=T, fig.height=3,fig.width=5}
library(knitr)
library(tidyr)
library(ggplot2)
set.seed(123)

for (i in 1:3){
  mdf <- data.frame(matrix(runif(25),5,5))
  cat(sprintf("htmlesc<<h1,Title %d>>\n",i))
  cat(sprintf("htmlesc<<h4,Smaller Title - also for %d>>\n",i))
  cat(sprintf("htmlesc<<p,and some text talking about this %d example>>\n",i))
  print(sapply(mdf,mean))
  gdf <- gather(mdf,series,val)
  gp <- ggplot(gdf)+geom_bar(aes(series,val,fill=series,color=I("black")),stat="identity")
  print(gp)
  print(mdf)
  plot(mdf)
}
```

这是输出(缩小了一点,因为您不需要详细信息)。

顺便说一句,唯一真正的文档是易辉的优秀编织书,搜索很容易找到。

【讨论】:

    【解决方案2】:

    您可以尝试使用kable 函数:

    ```{r, echo=T, results="asis"}
    library(knitr)
    for (i in 1:3){
      print("<h1>Title</h1>")
      #print("##Title")
      m <- data.frame(matrix(runif(25),5,5))
      print(kable(m, format = "html"))
    }
    ```
    

    这给了我:

    【讨论】:

    • 见我上面的评论。它实际上不是关于表格,而是关于 for 循环中的各种输出。这只是一个玩具样品。所以Kable并不是真正的前进方向。不过谢谢你的建议。
    【解决方案3】:

    试试这个。

    ```{r, echo=F, results="asis"}
    for (i in 1:3){
      library(knitr)
      print("<h1>Title</h1>")
      #print("##Title")
      m1 <- knitr::kable(data.frame(matrix(runif(25),5,5)))
      print(m1)
    }
    

    ```

    【讨论】:

    • 与上面 jeremycg 的建议相同。有同样的问题。我需要它用于一般输出,而不仅仅是 kable。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多