下面我提供了一个最小的可重现性示例。我目前正在尝试使用R quarto包将一个逻辑参数传递到Quarto块选项中。
下面是四方文档,其中我创建了2个参数,show_code和show_plot。
---
title: "Untitled"
format: html
params:
show_code: TRUE
show_plot: TRUE
---
```{r, echo=params$show_code}摘要(Cars)
```{r, include=params$show_plot}绘图(压力)
此文档将通过R studio中的“呈现”按钮正确地呈现。
但是,当试图通过R quarto包中的quarto_render()函数呈现该文档时,这将失败。
library(quarto)
quarto::quarto_render(
input = 'qmd_document.qmd',
output_format = 'html',
output_file = 'qmd_document_with_code.html',
execute_params = list(show_plot = TRUE,
show_code = TRUE)
)

似乎是将一个字符yes而不是逻辑TRUE或FALSE传递给控制台中的块1和块2。
如何通过quarto_render()正确地将逻辑字符传递给参数化Quarto以报告块选项
发布于 2022-09-01 17:11:05
选项1(在块头中使用块选项)
我们可以在块选项(就像我们在r-markdown中所做的那样)中使用逻辑语句。
parameterized_report.qmd
---
title: "Using Parameters"
format: html
params:
show_code: "yes"
show_plot: "yes"
---
```{r, echo=(params$show_code == "yes")}摘要(Cars)
```{r, include=(params$show_plot == "yes")}绘图(压力)
然后,在r-控制台/rscript文件中,我们使用params "yes"或"no"调用"yes",
quarto::quarto_render(
input = 'parameterized_report.qmd',
output_format = 'html',
output_file = 'qmd_document_with_code.html',
execute_params = list(show_plot = "yes",
show_code = "no")
)

选项2(使用YAML语法块选项)
请注意,上面我们在块头中使用了块选项,它可以很好地工作在针织引擎中,但是quarto更喜欢基于注释的yaml语法(即使用#|)。根据quarto文档
请注意,如果您愿意,仍然可以在第一行中包含块选项(例如`{r,echo = FALSE})。也就是说,我们建议使用基于注释的语法来使文档在执行引擎之间更加可移植和一致。块选项包括这样的方式,使用YAML语法而不是R语法来与YAML前端提供的选项保持一致。不过,您仍然可以用
!expr对选项值使用R代码作为前缀。
因此,按照四边形的首选方法,我们可以使用这样的参数,
parameterized_report.qmd
---
title: "Using Parameters"
format: html
params:
show_code: "false"
show_plot: "false"
---
```{r}#_ params$show_code _
摘要(Cars)
```{r}#\\包括:!expr params$show_plot
绘图(压力)
然后将quarto_render与"true"或"false"结合使用
quarto::quarto_render(
input = 'parameterized_report.qmd',
output_format = 'html',
output_file = 'qmd_document_with_code.html',
execute_params = list(show_plot = "true",
show_code = "true")
)最后,请注意两件事,
"true"或"false",因为在基于注释的语法中,echo、include、eval采用的是值true/false,而不是TRUE/FALSE。knitr 1.35,针织引擎也支持这种基于注释的语法。请参阅从针织v1.35到v1.37的新闻:块选项的替代语法https://stackoverflow.com/questions/73571919
复制相似问题