17

for ループを持つ R スクリプトに基づいて、knitr を使用して HTML レポートを生成しようとしています。forループ内のコメントからマークダウンコメントを生成したいのですが、可能かどうかわかりません。

簡単な例を次に示します。これは 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')

ただし、Rmd ファイルは次のようになります。

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

R チャンク内のマークダウン コメントは HTML にコンパイルされません。出来ますか?

ありがとう、ピーター

4

3 に答える 3

17

スピンに渡す R スクリプトの "#+" の後に指定できるコード チャンク オプション results='asis' を使用して、knitr で必要なものを取得できると思います (ただし、コードは興味深いものよりも「クリーン」に見えません)。 @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 ファイルは次のようになります。

## 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
于 2013-06-19T08:18:05.080 に答える
6

ting の前に実行したくない場合に、このような (および同様の) 問題に役立つ可能性がある、私のパンダー パッケージknitrに基づいて、@Yihui とは独立していくつかの機能を (再) 実装しました。クイックデモ:brewbrewknit

> 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このような面倒な設定を使用する必要はありません)。また、たとえば条件 (レポートの一部を表示する、またはレンダリングしないなど) にタグを使用することもできます。そして最も重要なことは、 (未処理の R コマンド) タグと(結果が によって処理される) タグの間には大きな違いがあることです。後者は、返されたすべての R オブジェクトが Pandoc のマークダウンに変換されることを意味します。text<% ... %><% ... %><%= ... %>pander

> 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_
于 2013-06-18T21:45:14.517 に答える