Option 1: include
If you want to include
other code snippets, you could only use qmd files when using include:
Computational includes work only in .qmd files
Let's create a simple qmd file called create_dataframe.qmd which add a column to mtcars:
---
title: "Create dataframe"
---
```{r}
# Create data file
mtcars$new_col = sample(c(0,1), nrow(mtcars), replace = TRUE)
```
Now you can include that file in your main.qmd file like this:
---
title: "Document"
---
{{< include create_dataframe.qmd >}}
Let's check if extra columns is created:
```{r}
# View dataframe
head(mtcars)
```
Output:

As you can see, the column created in the create_dataframe.qmd file is now in your main file.
Option 2: file in chunk
You could also the file
option in your chunk like this. You have to make sure it evaluates the code using eval
like this:
---
title: "Document"
---
Let's check if extra columns is created:
```{r}
#| echo: true
#| eval: true
#| file: create_dataframe.R
```
```{r}
# View dataframe
head(mtcars)
```
Output:

As you can see the new column is also in the dataframe.