【问题标题】:Rmarkdown's render() + knitr's spin(): How to mix code blocks and nested itemsRmarkdown的render() + knitr的spin():如何混合代码块和嵌套项
【发布时间】:2015-04-18 12:36:23
【问题描述】:

我正在使用rmarkdown 的函数render()knitr 的函数spin()R 脚本生成html 和pdf 笔记本。有时我使用嵌套列表并将它们与代码块混合。这是一个使用 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) 相同的缩进。可以这样做吗?

【问题讨论】:

    标签: r markdown knitr r-markdown


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您必须在文档中使用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个空格

      导致:

      我使用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    
      

      【讨论】:

      • 谢谢。直接用render("test.R", output_format = "pdf_document") 编译R 脚本时,我无法让它工作。
      • 你的文件应该命名为*.Rmd,而不是*.R,但除此之外:奇怪。对我来说它工作得很好:导致filedropper.com/test_50
      • render() 也可以用于纯的、普通的R-scripts,而不仅仅是*.Rmd 文件到笔记本。我的意思是像this 这样的脚本。它基于spin(),它将编译纯R-scripts 转换为*.Rmd 文件,这就是我所说的。可以在here 找到解释。 @Yihui,你能帮忙吗?
      猜你喜欢
      • 1970-01-01
      • 2018-12-10
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      相关资源
      最近更新 更多