我正在RStudio中创建一个quarto预订项目来呈现一个html文档。我需要在yml文件中指定一些参数,但是qmd文件返回"object 'params‘not“。使用knitR。
我使用默认的yml文件,其中在book标记下添加了params。
project:
type: book
book:
title: "Params_TEst"
author: "Jane Doe"
date: "15/07/2022"
params:
pcn: 0.1
chapters:
- index.qmd
- intro.qmd
- summary.qmd
- references.qmd
bibliography: references.bib
format:
html:
theme: cosmo
pdf:
documentclass: scrreprt
editor: visual
qmd文件如下所示
# Preface {.unnumbered}
This is a Quarto book.
To learn more about Quarto books visit <https://quarto.org/docs/books>.
```{r}
1+1
params$pcn
当我呈现书或在Rstudio中预览书时,我收到的错误是:
从第8-10行(index.qmd)错误中退出(expr,环境,附件):对象'params‘找不到调用:.main .withVisible -> eval_with_user_handlers -> ->
我已经尝试过在不同的地方将平行线放置在yml中,但是到目前为止没有什么效果。
有人能帮忙吗?
发布于 2022-07-16 17:55:16
目前,这似乎只有当你把参数添加到每一页的头版YAML。
如果您有大量的页面,并且需要保持参数集中,那么解决方法就是运行一个预处理脚本来替换所有页面中的参数。若要添加预处理脚本,请将键pre-render
添加到_quarto.yml
文件中。Quarto网站有详细说明。
例如,如果您有N个名为index<N>.qmd
的页面,则可以在每个页面的YML中设置一个占位符:
---
title: This is chapter N
yourparamplaceholder
---
您的预渲染脚本可以用所需的参数替换yourparamplaceholder
。下面是一个Python脚本示例:
for filename in os.listdir(dir):
if filename.endswith(".qmd"):
with open(filename, "r") as f:
txt = f.read()
f.replace('yourparamplaceholder', 'params:\n\tpcn: 0.1\n\tother:20\n')
with open(filename, "w") as ff:
ff.write(txt)
我同意你的观点,能够集中设置参数是个好主意。
发布于 2022-08-10 14:18:18
对于多页呈现,例如夸托书籍,您需要将YAML添加到每个页面,而不是在_quarto.yml文件中。
因此,在您的示例中,每个调用参数的章节都需要一个YAML头,比如index.qmd、intro.qmd和summary.qmd,但可能不需要references.qmd。
YAML头看起来应该像在标准Rmd中一样。例如,您的index.qmd应该如下所示:
---
params:
pcn: 0.1
---
# Preface {.unnumbered}
This is a Quarto book.
To learn more about Quarto books visit <https://quarto.org/docs/books>.
```{r}
1+1
params$pcn
But, what if you need to change the parameter and re-render? Then simply pass new parameters to the quarto\_render function
```javascript
Quarto::quarto_render(输入=这里::这里(“quarto”),#期待一个dir呈现
output_format = "html", #output dir is set in _quarto.yml
cache_refresh = TRUE,
execute_params = list(pcn = 0.2))
https://stackoverflow.com/questions/72992071
复制相似问题