使用Knitr将Markdown文档的部分插入另一个Markdown文档中 [英] insert portions of a markdown document inside another markdown document using knitr
问题描述
我知道这可以使用php和其他语言来完成,但是想知道是否可以使用knitr来完成以下工作:
I know this can be done with php and other languages, but was wondering whether the following could be accomplished using knitr:
假设我有一个Rmarkdown(.rmd)文档,其中包含两个标题1部分:
Let's say I have an Rmarkdown (.rmd) document with two heading 1 sections:
# 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:如果打开另一个.rmd文档,我如何创建链接,以便在分析该文档时可以显示其内容以及第一个文档的全部内容.例如:
Question 1: if open another .rmd document, how can i create a link so that when parsed this document would present its content as well as the whole content from the first document. For example:
# 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:knitr是否允许我选择文档1的选定部分并将其仅插入到文档2中?例如,仅标题1及其下方的内容,或仅标题2及其图形
Question 2: would knitr allow me to select and insert only selected portions of document 1 into document 2? For example, only heading 1 and the content below it, or only heading 2 and its plot
推荐答案
-
就是块选项
child
的用途,例如在second.Rmd
中,您可以
that is what the chunk option
child
is for, e.g. insecond.Rmd
, you can
```{r child='first.Rmd'}
```
有点棘手,但是您可以手动调用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')
```
这篇关于使用Knitr将Markdown文档的部分插入另一个Markdown文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!