4

I have read previously asked similar questions here, here, and here (among many more).

Unfortunately, none of the solutions offered to those questions seem to solve my issue.

I tried the function written by @bryanshalloway here but that did not have the desired result.

For more context, I am producing scientific manuscripts using an R Markdown workflow. I perform EDA in one notebook and later come back to write the manuscript in a different notebook. I import the data, wrangle it, create tables, and do some basic visualizations in the EDA notebook and include narrative text (mostly for myself).

I then create a separate notebook to write the manuscript. To keep it reproducible, I want to include all of the steps from the EDA with respect to data import, tidying, and wrangling, however I do not need the commentary that went along with it. Additionally, I may want some (but definitely not all) of the tables and basic visualizations I created during the EDA, but I would need to build them up substantially to get them publication ready.

My current workflow is just copying and pasting the relevant code chunks and then adding to those where necessary for tables and figures (i.e., adding labels and captions to a ggplot).

Is there a way to "source" these individual code chunks from one R Markdown file/R Notebook into another? Perhaps using knit_child (but not bring the entire R Markdown file into the current parent file)?

I would like to avoid copying the desired code chunks into separate R scripts.

Thanks!

  • Hi this should be pretty straightforward using the knitr::purl() function and then pasting into rmd (basically create a reverse purl function) (its spin() ) – user12256545 Dec 30 '21 at 18:27
  • why not make a separate Rmd with the chunk you are sharing and use the `child=file/path/RMd` option – rawr Dec 30 '21 at 19:39

1 Answers1

2

It is very possible with knitr purl and spin:

Ok lets say this is your initial Rmarkdown report: call the file report1.Rmd


    ---
    title: Use `purl()` to extract R code
    ---
    
    The function `knitr::purl()` extracts R code chunks from
    a **knitr** document and save the code to an R script.
    
    Below is a simple chunk:
    
    ```{r, simple, echo=TRUE}
    1 + 1
    ```
    
    Inline R expressions like `r 2 * pi` are ignored by default.
    
    If you do not want certain code chunks to be extracted,
    you can set the chunk option `purl = FALSE`, e.g., 
    
    ```{r, ignored, purl=FALSE}
    x = rnorm(1000)
    ```

Then you go to the console and purl the file:

> knitr::purl("report1.Rmd")

this creates an R file called report1.R in the same directory you are in, with only the chunks that are not purl=false. Its an simple R script looking like this:

## ---- simple, echo=TRUE----------------------------------------------------------------------------
1 + 1


Lets rename the file for safety purposes:

> file.rename("report1.R", "report_new.R")

Finally lets spin it back to report_new.Rmd :

> knitr::spin("report_new.R", format = "Rmd", knit=F)

This gives you a new Rmd file called report_new.Rmd containing only the relevant chunks and nothing else

    ```{r simple, echo=TRUE}
    1 + 1 
    ```
user12256545
  • 2,755
  • 4
  • 14
  • 28
  • Source : purl: https://bookdown.org/yihui/rmarkdown-cookbook/purl.html spin: https://bookdown.org/yihui/rmarkdown-cookbook/spin.html – user12256545 Dec 30 '21 at 19:25