如果您想要包含
其他代码片段,使用包含时只能使用 qmd 文件:
计算包含仅在 .qmd 文件中工作
让我们创建一个名为create_dataframe.qmd的简单 qmd 文件,它将向 mtcars 添加一列:
---
title: "Create dataframe"
---
```{r}
# Create data file
mtcars$new_col = sample(c(0,1), nrow(mtcars), replace = TRUE)
```
现在你可以像这样在你的 main.qmd 文件中包含那个文件:
---
title: "Document"
---
{{< include create_dataframe.qmd >}}
Let's check if extra columns is created:
```{r}
# View dataframe
head(mtcars)
```
输出:
正如您所看到的,create_dataframe.qmd 文件中创建的列现在已经在您的主文件中了。
您也可以在代码块中使用file
选项。您必须确保使用eval
来评估代码,就像这样:
---
title: "Document"
---
Let's check if extra columns is created:
```{r}
#| echo: true
#| eval: true
#| file: create_dataframe.R
```
```{r}
# View dataframe
head(mtcars)
```
输出:
正如您所看到的,新列也在数据框中。
knitr::read_chunk("Ch02.R")
只是将外部文件中的R
代码片段包含到Quarto
文档中的一种解决方案。 - MYaseen208