【问题标题】:insert portions of a markdown document inside another markdown document using knitr使用knitr将markdown文档的一部分插入另一个markdown文档中
【发布时间】:2013-07-11 12:53:06
【问题描述】:

我知道这可以用 php 和其他语言完成,但想知道是否可以使用 knitr 完成以下操作:

假设我有一个包含两个标题 1 部分的 Rmarkdown (.rmd) 文档:

# This is the first heading for the first document
Lorem ipsum dolor sit amet

# This is the second heading for the first document
plot(object)
  1. 问题 1:如果打开另一个 .rmd 文档,我如何创建一个链接,以便在解析该文档时显示其内容以及第一个文档的全部内容。例如:

    # This is the first heading for the second document
    Lorem ipsum dolor sit amet
    
    [command goes here to insert the first document]
    

    结果是:

    # This is the first heading for the second document
    Lorem ipsum dolor sit amet
    
    # This is the first heading for the first document
    Lorem ipsum dolor sit amet
    
    # This is the second heading for the first document
    [plot shows up here]
    
  2. 问题 2:knitr 是否允许我仅选择文档 1 的选定部分并将其插入到文档 2 中?比如只有标题1和它下面的内容,或者只有标题2和它的情节

【问题讨论】:

  • 您可以通过子文档做第一件事。见this。第二个比较棘手,取决于您如何定义应该显示的内容和不应该显示的内容。

标签: r markdown knitr r-markdown


【解决方案1】:
  1. 这就是块选项child 的用途,例如在second.Rmd,你可以

    ```{r child='first.Rmd'}
    ```
    
  2. 这有点棘手,但您可以手动拨打knit_child(),例如

    ```{r echo=FALSE, results='asis'}
    # knit the first three lines of first.Rmd
    cat(knit_child(text = readLines('first.Rmd')[1:3]), sep = '\n')
    ```
    

【讨论】:

  • 请注意,子 md 文档中的 1 级标题将在 bookdown 中编成单独的章节。
猜你喜欢
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-22
  • 2016-02-18
  • 1970-01-01
相关资源
最近更新 更多