Knitr HTML Loop-一些HTML输出,一些R输出 [英] Knitr HTML Loop - Some HTML output, some R output

查看:76
本文介绍了Knitr HTML Loop-一些HTML输出,一些R输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历一个列表,并用HTML打印其中的一部分,并以代码的形式打印. 因此更精确:我想产生与创建时相同的输出

i want to loop through a list and and print some part of it in HTML and some as Code. So be more precise: I want to produce the same output this is creating

<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> 

我设法将的结果打印为HTML,但结果也直接以HTML打印, 带有以下块:

I managed to print the 's to HTML but the results are printed directly in HTML as well, with the following 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-->

会对所有建议感到非常高兴.

Would be very happy about all suggestions.

PS:之所以要设置格式,是因为 knirtBootstrap 然后会产生一个非常漂亮的输出

P.S.: The reason why i want to have the formatting is that knirtBootstrap then produces a very nice Output.

推荐答案

您好,再次使用两个.Rhtml文件查找另一个解决方案.第一个mainfile.Rhtml会根据需要多次调用第二个.在stepfile.Rhtml中,您可以根据需要放置块.您只需要编译mainfile.Rhtml.

Hello again Floo0 an other solution using two .Rhtml files. The first one, mainfile.Rhtml, calls the second one as many time you want. In stepfile.Rhtml you can put chunks as you want. You just have to compile 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-->

我从>针织衫大块

这篇关于Knitr HTML Loop-一些HTML输出,一些R输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆