我使用外部 JSON 文件来存储 Quarto 文档中使用的文本。正在读入此 JSON 文件,然后我使用它来填充文本。我想将 R 代码嵌入到字符串中,类似于在四开本中使用由刻度线包围的裸文本所做的事情。
这可以在执行 R 代码的地方完成吗
{
"about": {
"title": "The mean is `r mean(1:5)`"
}
}
```{r}
info <- jsonlite::read_json("report_text.json")
```
This works but I am not using the JSON: The mean is `r mean(1:5)`
This does not work, this is what I want to do: `r info$about$title`
```{r, results='asis'}
# This does not work either
cat(info$about$title)
```
从技术上讲,您可以使用
include
短代码和临时文件。---
format: html
---
``` {r}
#| echo: FALSE
info <- jsonlite::read_json("report_text.json")
readr::write_file(info$about$title, "_info_about_title.qmd")
```
{{< include _info_about_title.qmd >}}
您可以使用
knitr::knit_child
来解析文本块
```{r, results='asis', echo=FALSE}
# This does not work either
cat(knitr::knit_child(text=info$about$title, quiet=TRUE))
```