Rmarkdown的render()+ knitr的spin():如何混合代码块和嵌套项

时间:2015-04-18 12:36:24

标签: r markdown knitr r-markdown

我使用R的函数rmarkdownrender()'来自knitr生成html和pdf-notebooks。功能spin()。有时我使用嵌套列表并将它们与代码块混合使用。以下是使用rmarkdownknitr块选项的示例。

#' (1) This is normal text.
#'    (a) This is normal text but indented.
#+ echo = TRUE, eval = TRUE 
print("This is code")
#'    (b) This is supposed to be normal text with the same
#'        indentation as (a). However, it will be formatted as code.
#'        By this I mean that e.g. in a pdf-notebook it will be 
#'        correctly indented but the font will be the same font as 
#'        the code.

但是,列表项(a)之后的代码后面的所有内容也将被标记为代码(例如(b))。但我想要实现的是将(b)标记为普通文本并使用与(a)相同的缩进。有可能这样做吗?

2 个答案:

答案 0 :(得分:4)

您必须在文档中使用所谓的The four-space rulehttp://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#the-four-space-rule

所以下面的代码可以运行

  (1) This is normal text.

    Continued.

    (a) This is normal text but indented.

        ```{r, echo = TRUE, eval = TRUE} 
        summary(cars)
        ```

    (a) This is normal text with the same indentation as (a).

注意:

  • (1)
  • 前面的2个空格
  • 每个(a)前面的4个空格
  • 代码块前面的8个空格

导致: enter image description here

我使用rmarkdown::render("test.Rmd")运行它,这是我的会话信息

R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] digest_0.6.8    evaluate_0.5.5  formatR_1.0     htmltools_0.2.6 knitr_1.9       rmarkdown_0.5.1
 [7] stringr_0.6.2   tools_3.1.1     XML_3.98-1.1    yaml_2.1.13    

答案 1 :(得分:4)

有一个内部块选项indent可以为块输出添加缩进。在您的情况下,您可以指定四个空格,例如

#+ echo = TRUE, eval = TRUE, indent = '    '