2013-06-18 51 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")必須在最後,它不會,即使你的情節工作或者在循環中計算一些東西。