在for循环中生成markdown注释 [英] generate markdown comments within for loop

查看:153
本文介绍了在for循环中生成markdown注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用knitr基于具有for循环的R脚本生成HTML报告.我想从for循环中的注释生成markdown注释,但不确定是否可行.

I am trying to generate an HTML report, using knitr, based on an R script that has for loops. I want to generate markdown comments from the comments within the for loop, but I am not sure if it's possible.

这是一个简单的示例,这在test.R中:

Here is simple example, this is in test.R:

for (i in 1:5) {
    ## This is a heading for `i`
    #' This is a comment for `i`
    print(i)    
}

然后我使用spin生成一个Rmd文件: spin('test.R')

Then i use spin to generate a Rmd file: spin('test.R')

但是,Rmd文件如下所示.

However, the Rmd file looks like the following.

```{r }
for (i in 1:5) {
    ## This is a heading for `i`
    #' This is a comment for `i`
    print(i)    
}
```

R块中的markdown注释未编译为HTML.有可能吗?

The markdown comments within the R chunk are not compiled into HTML. Is it possible?

谢谢, 彼得

推荐答案

我认为您可以使用R脚本中#+"之后指定的代码块选项result ='asis'在knitr中获得所需的内容.传递给旋转(但是代码看起来比@daroczig提出的有趣的brew解决方案干净"):

I think you can obtain what you want in knitr with the code chunk option results='asis' that you can specify after "#+" in an R script to be passed to spin (but the code looks less "clean" than the interesting brew solution proposed by @daroczig):

#+ results='asis', echo = FALSE
for (i in 1:5) {
    cat("## This is a heading for ", i, "\n")
    cat("<!-- This is a comment for ", i, "-->\n")
    print(i)    
}

如果这是test.R脚本,并且您进行了spin("test.R"),则生成的md文件将如下所示:

If this is test.R script and that you do spin("test.R"), the resulting md file will look like that :

## This is a heading for  1 
<!-- This is a comment for  1 -->
[1] 1
## This is a heading for  2 
<!-- This is a comment for  2 -->
[1] 2
## This is a heading for  3 
<!-- This is a comment for  3 -->
[1] 3
## This is a heading for  4 
<!-- This is a comment for  4 -->
[1] 4
## This is a heading for  5 
<!-- This is a comment for  5 -->
[1] 5

这篇关于在for循环中生成markdown注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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