0

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.

shafee
  • 15,566
  • 3
  • 19
  • 47
kputschko
  • 766
  • 1
  • 7
  • 21

3 Answers3

4

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


contents of lm.R

model <- lm(am ~ cyl + mpg, data = mtcars)
model
shafee
  • 15,566
  • 3
  • 19
  • 47
  • Awesome. Thanks! Do you know where I can read more about the class-output options? – kputschko Oct 21 '22 at 18:37
  • Well I have known this option as [a `knitr` option](https://yihui.org/knitr/options/) from using Rmarkdown and [quarto also has this](https://quarto.org/docs/reference/cells/cells-knitr.html#cell-output). So if you look at the underlying html structure of the document for example, you will see quarto uses `sourceCode r` class for code chunks. So that's why I have used this for `class-output` to get the same effect. – shafee Oct 21 '22 at 18:47
2

I was successful in Quarto and pdf with this solution:

```{r}
#| echo: true
#| eval: false # skip evaluation
#| file: C:/directory/script.R
```

See: https://stackoverflow.com/a/75315426/14361772

1

You can also use the Quarto extension include-code-files and skip the readLines and cat calls.

Install the extension from: https://github.com/quarto-ext/include-code-files

---
title: Reference Document
filters:
   - include-code-files
---

# Linear Model Scripts

Here is the script we used for linear models:

```{.markdown include="lm.Rmd"}
```

# More Scripts
...

This method works for other source code types too. See this list