Home > database >  How can I include an external RMD file in a Quarto qmd script without evaluating the external script
How can I include an external RMD file in a Quarto qmd script without evaluating the external script

Time:10-22

I'm creating a reference document with snippets of code from various Rmd files.

For example, my first file looks like this:

lm.Rmd:

---
title: Linear Models
---

# How to Run Linear Models

```{r}
lm(am ~ cyl   mpg, data = mtcars)
```

## Linear Model Results
...

# A New Linear Model
...

and then in a second file (using Quarto qmd now), I'm trying to reference this external script and have it be displayed as raw Rmd code, not as HTML or Markdown output. Here's what I'm trying:

reference.qmd:

---
title: Reference Document
---

# Linear Model Scripts

Here is the script we used for linear models:

```{r}
#| file: dir/lm.Rmd
#| eval: false
#| echo: true
#| code-folding: true
```

# More Scripts
...

What I want is the raw RMD/QMD code to be included in the 'echoed' code box on the final reference.qmd HTML document. But what is happening is that the markdown from the lm.Rmd file is being evaluated and is getting displayed as if I had written it directly in the reference.qmd document.

In the end, what I want is to be able to display any arbitrary external script and have the end-user be able to scroll through that script in the final HTML output (a QMD book in this case). The file: option in the QMD code chunk seems to get me partially there, as it works well for .R files, but it doesn't seem to work for .Rmd files.

CodePudding user response:

You can read the file content with readLines and cat it as output. And then use sourceCode r class for output class to get the output styled as R code chunk.

---
title: Reference Document
---

## Linear Model Scripts

Here is the script we used for linear models:


```{r}
#| echo: false
#| warning: false
#| class-output: "sourceCode r"

cat(readLines("lm.Rmd"), sep = "\n")
```

## More Scripts

```{r}
#| echo: false
#| warning: false
#| class-output: "sourceCode r"

cat(readLines("lm.R"), sep = "\n")
```

added file content without evaluating it


lm.R

model <- lm(am ~ cyl   mpg, data = mtcars)
model
  • Related