2013-06-18 49 views
12

我正在尝试使用knitr基于具有for循环的R脚本生成HTML报告。我想从for循环中的评论生成降价评论,但我不确定是否可能。在for循环中生成降价评论

这里是简单的例子,这是在test.R:

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

然后我使用旋以产生RMD文件: 自旋( 'test.R')

然而,RMD文件如下所示。

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

R组块中的Markdown注释未编译为HTML。可能吗?

谢谢, 彼得

+0

它听起来像你想在转换到Rmd之前运行一些R代码,而据我所知,它旋转(接下来是编织)是否相反。我认为brew模板可能对此有所帮助。 – baptiste

+1

我认为你的意思是_roxygen_而不是_Markdown_评论。正如@baptiste提到的,'brew'对于这类任务来说更复杂(从循环中生成文本)。 –

+0

@易辉,你是对的。 R文件包含roxygen注释,我希望在运行旋转后将其转换为Markdown注释。 – pmichaels

回答

5

我(重新)实现在我pander包基于brew从@Yihui knitr独立一些功能,可以与这些(以及类似的)问题,帮助,如果你不想跑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_ 

请注意,你也可以传递一个文件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_ 
+0

感谢您的建议。我希望避免在R代码中使用太多显式标签,但很高兴知道有一个解决方法。 – pmichaels

9

我认为你可以得到你的代码块的选择结果=“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)  
} 

如果这是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 
+0

这允许我将注释放入输出中,但注释未使用Markdown符号格式化。 – pmichaels

+0

你是什么意思的评论格式与减价表示法?这是你正在寻找的html评论?如果是的话,你可以用同样的方法做,看看我编辑的例子 – Gilles

+0

(+1)为了显示标题2到5,我需要在'print(i)'后加一个'cat('\ n')'作为标题。 – jbaums

4

为我工作的一种解决方案由how to create a loop that includes both a code chunk and text with knitr in R提供。通过使用两个results='asis'和在每个循环的末尾\n前面的两个空格。

例如:

没有两个空间

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

输出(HTML):

enter image description here

正如你可以看到,注释和标题搞的一团糟一起

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

for (i in headers){ 
    cat("\n##H ", i, "\n") 
    cat("comment",i) 
    cat(" \n")# <--------------------------------- 
} 

enter image description here

纸条末尾:cat(" \n")必须在最后,它不会,即使你的情节工作或者在循环中计算一些东西。