Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

r - insert portions of a markdown document inside another markdown document using knitr

I know this can be done with php and other languages, but was wondering whether the following could be accomplished using knitr:

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. 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]
    

    result would be:

    # 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. 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. that is what the chunk option child is for, e.g. in second.Rmd, you can

    ```{r child='first.Rmd'}
    ```
    
  2. that is a little bit trickier, but you can call knit_child() manually, e.g.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...