3

I have several R scripts that are are documented using #' with the purpose of combining all the scripts into a single .Rmd file.

I saw from this post that it's really straight forward to combine multiple .Rmd files using code chunks within the master .Rmd file

This is nice but I prefer to keep my code as .R files because it runs faster for its intended purpose and the rendering of the documentation will not happen as often.

First I tried this in the main markdown file:

```{r, child = "script.R"}
```

But that didn't render properly - bascally a bunch of markdown text with the #'s present.

Then I attempted to use what's described in this blog post in order to combine the R scripts in a single markdown file:

```{r}
library(rmarkdown)
rmarkdown::render("script.R")
```

But this just produces script.md and does not embed the markdown into the main file. Any thoughts on how to correctly render the .R scripts as markdown within the main file?

Warner
  • 1,353
  • 9
  • 23

1 Answers1

2

This is my approach. It will use rmarkdown::render to generate md file, and then read the content of the md file and incorporate it into the main file by setting the option results to asis. The drawback is the method generates some temporary files, and it may not be very performant but it achieves the goal.

---
title: "test"
author: "Consistency"
date: "2017/6/29"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

join <- function(ls, sep = ", "){
    do.call(paste, append(ls, list(sep = sep)))
}

inline_render <- function(script_name){
    suppressMessages(capture.output(rmarkdown::render(paste0(script_name, ".R"), output_format = "rmarkdown::md_document"), file = "tmp"))
    cat(join(readLines(paste0(script_name, ".md")), "\n"))

}
```

```{r script, echo=FALSE, results='asis'}
inline_render("script")
```

```{r script1, echo=FALSE, results='asis'}
inline_render("script1")
```
Consistency
  • 2,884
  • 15
  • 23
  • Thanks for the answer - this works for me but it appears to make diagrams within my markdown stop displaying. Specifically diagrams created using `grViz()` function and the `diagrammeR` package – Warner Jun 30 '17 at 17:07
  • That is a somewhat unrelated problem. But one problem I'm encountering is when add other scripts is this error `Quitting from lines NA-87 (script2.spin.Rmd) Error in parse_block(g[-1], g[1], params.src) : duplicate label 'unnamed-chunk-1'` – Warner Jun 30 '17 at 17:37
  • @Warner It seems that the second problem is caused by label conflict, so a solution is to label the code chunk, like in my edited answer. – Consistency Jun 30 '17 at 18:08
  • 1
    @Warner Could you provide some mimic example of your `DiagrammeR` code so I can test with? Thx. – Consistency Jun 30 '17 at 18:12
  • thanks I actually just discovered that solution. I appreciate you putting it in your answer. Regarding my other problem, it seems that since my diagrams are `htmlwidget` objects and this seems to run into other kinds of issues probably out of the scope of my original question. – Warner Jun 30 '17 at 18:13
  • Here is an example to add to the markdown ` ```{r diagram, echo=FALSE} library(DiagrammeR) grViz("digraph { node [shape = circle] a, b edge[color=grey] a -> b}") ``` ` – Warner Jun 30 '17 at 18:16
  • 1
    @Warner Since the result generated from `DiagrammeR` is `htmlwidget`, so a possible solution is to modify the `inline_render` function, so the output is `html_document` instead of `md_document`, and also `cat(join(readLines(paste0(script_name, ".html")), "\n"))` instead of `cat(join(readLines(paste0(script_name, ".md")), "\n"))`. Then it seems to work. – Consistency Jun 30 '17 at 18:31
  • Yes this worked for me! Thanks a lot for helping me out with this! – Warner Jun 30 '17 at 18:47