Knitr HTML Loop - 一些HTML输出,一些R输出

Ren*_*rop 2 r knitr

我想循环一个列表,并用HTML打印一些部分,一些打印为代码.所以更精确一点:我想生成相同的输出

<h2> 1 is a great number </h2> 
<!--begin.rcode echo=FALSE print(rnorm(5,mean=1)) end.rcode--> 
<h2> 2 is a great number </h2> 
<!--begin.rcode echo=FALSE print(rnorm(5,mean=2)) end.rcode-->
...
<h2> x is a great number </h2> 
Run Code Online (Sandbox Code Playgroud)

我设法打印到HTML,但结果也直接打印在HTML中,使用以下Chunk:

<!--begin.rcode, echo=FALSE, results = 'asis'
for (i in list(1,2)){
   cat("<h2>", i, "is a great number</h2>")
   print(rnorm(5,mean=i))
}
end.rcode-->
Run Code Online (Sandbox Code Playgroud)

对所有建议都很满意.

PS:我想要格式化的原因是knirtBootstrap然后产生一个非常好的输出.

Vic*_*orp 5

你好再次使用两个.Rhtml文件的另一个解决方案Floo0.第一个,mainfile.Rhtml在你想要的时候调用第二个.在stepfile.Rhtml你可以根据需要放入块.你只需要编译mainfile.Rhtml.

## mainfile.Rhtml

<!--begin.rcode echo=FALSE
J <- 10
end.rcode-->


<!--begin.rcode include=FALSE
out <- NULL
for (i in 1:J) {
  out <- c(out, knit_child('stepfile.Rhtml'))
}
end.rcode-->


<!--rinline paste(out, collapse = '\n') -->


## stepfile.Rhtml

<!--begin.rcode echo=FALSE, results='asis'
cat("<h2>", i, "is a great number</h2>")
end.rcode-->

<!--begin.rcode echo=FALSE
print(rnorm(5,mean=i))
end.rcode-->
Run Code Online (Sandbox Code Playgroud)

我把这个想法从Dynamic的调用数量带到了一个带有knitr的块