You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于R变量值控制Quarto代码块的执行?

解决方案

问题出在Quarto代码块选项的语法上,要基于R变量动态控制代码块是否执行,需要用!expr前缀触发R代码解析,而非直接使用`r OK`。

修改后的Quarto文档如下:

---
title: "Test"
format: html
---

## Set a variable

```{r}
OK <- FALSE

Running Code depending on value of OK

#| eval: !expr OK
print("OK is TRUE - otherwise you won't see anything here.")
### 原理说明
- 在Quarto代码块选项中,`!expr`用于标记后续内容是需要执行的R表达式,而非普通字符串。
- 渲染时Quarto会先解析`OK`变量的布尔值,再以此判断是否执行当前代码块。
- 当`OK`设为`TRUE`时,代码块正常执行并输出内容;设为`FALSE`时,代码块会被跳过,无任何输出。

内容的提问来源于stack exchange,提问作者Rainer
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 15:10:28