在for循环中生成markdown注释

pmi*_*els 16 markdown r roxygen knitr

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

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

for (i in 1:5) {
    ## This is a heading for `i`
    #' This is a comment for `i`
    print(i)    
}
Run Code Online (Sandbox Code Playgroud)

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

但是,Rmd文件如下所示.

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

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

谢谢,彼得

Gil*_*les 13

我认为您可以使用代码块选项结果='asis'获取您想要的knitr中的内容,您可以在R脚本中将"#+"指定后传递给旋转(但代码看起来不像有趣的那样"干净" @daroczig提出的brew解决方案):

#+ 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)    
}
Run Code Online (Sandbox Code Playgroud)

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

## 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
Run Code Online (Sandbox Code Playgroud)

  • (+1)我需要在`print(i)`之后添加一个额外的`cat('\ n')`,以便将标题2到5呈现为标题。 (2认同)

dar*_*zig 6

我已(再)来实现的一些功能knitr基于独立@Yihui brew奉迎包,可以用这样的(以及类似的)问题的帮助,如果你不希望运行brew之前knit婷.快速演示:

> Pandoc.brew(text = "# Demonstrating a nice loop
+ <% for (i in 1:5) { %>
+ ## This is a header for <%=i%>
+ #' This is a comment for <%=i%>
+ <% } %>")

# Demonstrating a nice loop

## This is a header for _1_
#' This is a comment for _1_

## This is a header for _2_
#' This is a comment for _2_

## This is a header for _3_
#' This is a comment for _3_

## This is a header for _4_
#' This is a comment for _4_

## This is a header for _5_
#' This is a comment for _5_
Run Code Online (Sandbox Code Playgroud)

请注意,你也可以传递一个文件Pandoc.brew(不需要使用text带有实际问题的参数的麻烦设置),并且你也可以使用<% ... %>标签作为条件(如显示或不显示报告的一部分).最重要的是<% ... %>:(未处理的R命令)和<%= ... %>(结果由处理pander)标签之间存在巨大差异.后者意味着所有返回的R对象都转换为Pandoc的降价,例如:

> Pandoc.brew(text = "# Demonstrating a conditional
+ <% for (i in 1:5) { %>
+ ## This is a header for <%=i%>
+ <% if (i == 3) { %>
+ Hey, that's **almost** <%=pi%>, that's between <%=3:4%>! Wanna fit a model to _celebrate_?
+ <%= lm(mpg ~ hp, mtcars) %>
+ <% }} %>")
# Demonstrating a conditional

## This is a header for _1_

## This is a header for _2_

## This is a header for _3_

Hey, that's **almost** _3.142_, that's between _3_ and _4_! Wanna fit a model to _celebrate_?

--------------------------------------------------------------
     &nbsp;        Estimate   Std. Error   t value   Pr(>|t|) 
----------------- ---------- ------------ --------- ----------
     **hp**        -0.06823    0.01012     -6.742   1.788e-07 

 **(Intercept)**     30.1       1.634       18.42   6.643e-18 
--------------------------------------------------------------

Table: Fitting linear model: mpg ~ hp

## This is a header for _4_

## This is a header for _5_
Run Code Online (Sandbox Code Playgroud)


use*_*238 6

一个对我有用的解决方案是如何创建一个包含代码块和R中带有knitr的文本的循环.通过在每个循环的末尾使用两个 results='asis'和两个空格\n.

例:

没有两个空格:

```{r, results='asis'}
headers <- list("We","are","your","friends")
for (i in headers){
  cat("\n##H ", i, "  \n")
  cat("comment",i)
}
Run Code Online (Sandbox Code Playgroud)

输出(html):

在此输入图像描述

正如您所看到的,评论和标题混杂在一起

解决方案: 有两个空格:cat(" \n")在循环结束时

for (i in headers){
  cat("\n##H ", i, "\n")
  cat("comment",i)
  cat("  \n")# <---------------------------------
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

注意:cat(" \n")需要在最后,即使你在循环中绘制或计算它,它也不起作用.