单行的R标记块内的HTML代码

单行的R标记块内的HTML代码,html,r,regex,knitr,r-markdown,Html,R,Regex,Knitr,R Markdown,我在for循环中有一个R-markdown文档(测试各种类型的模型),我想用HTML头来设置它们,因为在其他方面很难找到我正在寻找的模型。有“asis”选项,但它会关闭整个块的格式,这不是我想要的。我尝试了一些我在这里发现的东西,但没有什么真正起作用。这是我的密码: --- title: "Untitled" author: "Mike Wise - 25 Jul 2014" date: "November 2, 2015" output: html_document --- Test ``

我在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)
}
```
---
标题:“无标题”
作者:“Mike Wise-2014年7月25日”
日期:“2015年11月2日”
输出:html\u文档
---
试验
```{r,echo=T}
(我在1:3中){
印刷品(“标题”)
#打印(“标题”)

m您可以尝试使用
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"))
}
```
`{r,echo=T,results=“asis”}
图书馆(knitr)
(我在1: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)
}
`{r,echo=F,results=“asis”}
(我在1:3中){
图书馆(knitr)
印刷品(“标题”)
#打印(“标题”)

m1好的,一年半过去了,我仍然时不时地需要它,但从那以后我学到了足够多的知识,知道如何正确地完成它。你需要编写一个
knitr
“输出钩子”,并修改输出,以便html可以“转义”通过

以下内容可以实现这一点:

  • 添加了一个
    knitr
    输出挂钩
  • 定义语法以指定所需的标记和内容
    • 例如,要获取
      某些文本
      请使用
      htmlesc
  • 找到一个regexp,它提取
    h1
    一些文本
    ,并将其重新格式化为正确标记的html,同时删除插入的
    knitr
  • 添加了更多的测试用例,以确保它做了一些额外的事情(如
    h4
    p
    、正确放置绘图和表格等)
  • 添加了另一个regexp以删除双转义行,该行添加了一些不需要的空白面板结构
下面是代码:

---
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)
}
```
---
标题:“HTML转义的输出挂钩”
作者:“某人”
日期:“2017年04月25日”
输出:
html_文件:
保持正确
---
```{r设置,include=T,echo=TRUE}
knitr::opts_chunk$set(echo=TRUE)

hook_输出为什么不使用
kable(m,format=“html”)
并保留asis?对于表和我考虑过的一些东西,这是可以的,但在这个应用程序中,打印出来的这些东西是不同算法的收敛消息,即不是表,对于for循环的每个迭代都不一样。因此,这不是一个真正的选项。\n如果您有来自模型拟合结果的收敛消息,您可以想看看broom::tidy,将模型拟合结果转换为数据。框架,然后可以用kable显示。嗯,真的有很多东西。收敛消息、摘要、混淆矩阵,所有我可能发现对故障排除和/或算法之间的选择有用的东西。我有点想让它保持稳定例如。但是如果没有办法按照我的要求去做,那么我想我将不得不沿着这条路走下去。如果它不是数据帧输出,您可以包括一个您确实拥有的输出的示例。请参阅我上面的评论。它不是真的关于表,而是关于for循环中的各种输出。这只是一个玩具示例。因此Kable不是真正的方法转发。但是谢谢你的建议。和上面jeremycg的建议一样。有同样的问题。我需要它为一般输出工作,而不仅仅是kable。