R-markdown块内的HTML代码(单行) [英] HTML code inside of a R-markdown block for a single line
问题描述
我在for循环中有一个R-markdown文档(测试各种模型),我想用HTML标头将它们关闭,因为否则很难找到我要寻找的模型.有"asis"
选项,但是关闭了整个块的格式化,这不是我想要的.我已经尝试了一些在这里找到的东西,但是没有任何效果.这是我的代码:
I have an R-markdown document in a for loop (testing various kinds of models), and I would like to set them off with HTML Headers, as it is otherwise hard to find the models I am looking for. There is the "asis"
option, but that turns off formatting for the entire block, which is not what I want. I have tried a few things I found here, but nothing really works. Here is my code:
---
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)
}
```
这里是一种标题格式不正确的尝试:
Here is one try that does not have the right title formatting:
这是results="asis"
选项的外观:
推荐答案
好吧,是一年半以后,我仍然不时需要这样做,但是从那时起我学到了足够的知识,知道如何正确地做到这一点. .您需要编写一个knitr
输出挂钩",并修改输出,以便html可以转义"通过.
Okay, it is a year and a half later and I still needed this from time to time, but I learned enough since then to know how to do it properly. You need to write a knitr
"output hook", and modify the output so the html can "escape" through.
以下是完成此任务的方法:
The following accomplishes this:
- 添加了
knitr
输出挂钩. - 定义了一种语法,以指定所需的标签和内容
- 例如要获取
<h1>some_text</h1>
,请使用htmlesc<<(h1,some_text)>>
- Added a
knitr
output hook. - Defined a syntax to specify the needed tag and content
- for example to get
<h1>some_text</h1>
usehtmlesc<<(h1,some_text)>>
下面是代码:
--- 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) } ```
这是输出(由于不需要详细信息,因此会缩小一点).
And this is the output (shrunk a bit as you don't need the details).
顺便说一句,唯一真正的文档是Yihui出色的针织书籍,通过搜索即可轻松找到.
The only real docs for this by the way are Yihui's excellent knitr book, a search finds it easily.
这篇关于R-markdown块内的HTML代码(单行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- for example to get
- 例如要获取