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

如何在.RMD文件中动态添加Pandoc/HTML代码重复生成布局?

问题描述

我在.RMD文件渲染HTML时,想要动态生成Pandoc或HTML代码,实现左侧文本、右侧图表的两栏布局,接着展示一个全宽图表,且该布局需要重复n次。目前已能手动实现单次布局,但循环生成的尝试存在问题,附上相关代码示例,同时接受HTML代码的替代解决方案。


原单次可正常运行的布局代码

---
output: html_document
---
# 开始:需要重复n次的部分

:::: {style="display: flex;"}

::: {}
Here is the **first** Div.

```{r echo = FALSE}
cat(paste0("Here is some text that I would like to display on the left"))

:::

::: {}
And this block will be put on the right:

plot(iris[, -5])

:::

::::

Then something that takes up the entire space

plot(cars)
结束:需要重复n次的部分
---

## 我的尝试代码(存在问题)
```r
n = 4
generate_pattern <- function() {
  cat(paste0(":::: {style=\"display: flex;\"}\n"))
  cat(paste0("::: {}\n"))
  cat(paste0("Here is the **first** Div.\n"))
  cat(paste0("```{r echo = FALSE}\n"))
  cat(paste0("cat(paste0(\"Here is some text that I would like to display on the left\"))\n"))
  cat(paste0("```\n"))
  cat(paste0("::: {}\n"))
  cat(paste0("And this block will be put on the right:\n"))
  cat(paste0("```{r echo = FALSE}\n"))
  cat(paste0(plot(iris[, -5]), "\n"))
  cat(paste0("```\n"))
  cat(paste0("::: \n"))
  cat(paste0(plot(cars)))
}

# Generate the pattern n times
for (i in 1:n) {
  generate_pattern()
}

问题分析与解决方案

问题根源

  1. 直接调用plot()并通过cat()输出错误:plot()会直接在当前设备绘图,而非生成可被knitr解析的代码内容,导致输出乱码或无有效图表。
  2. 布局标签闭合不全:代码中缺少两栏布局的闭合标签::::,全宽图表的代码块也未正确生成。
  3. 未启用results='asis':动态生成的Markdown/HTML代码需要让knitr直接输出到文档,而非作为R代码结果显示。

方案一:基于Pandoc Div的动态生成

将循环代码放在results='asis'的R代码块中,生成可被正确解析的Markdown代码:

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
n <- 4

generate_pattern <- function() {
  # 两栏布局容器
  cat(":::: {style=\"display: flex; gap: 20px;\"}\n\n")
  
  # 左侧文本栏
  cat("::: {style=\"flex: 1;\"}\n")
  cat("Here is the **first** Div.\n\n")
  cat("```{r}\n")
  cat("cat(paste0(\"Here is some text that I would like to display on the left\"))\n")
  cat("```\n")
  cat(":::\n\n")
  
  # 右侧图表栏
  cat("::: {style=\"flex: 1;\"}\n")
  cat("And this block will be put on the right:\n\n")
  cat("```{r}\n")
  cat("plot(iris[, -5])\n")
  cat("```\n")
  cat(":::\n\n")
  
  # 闭合两栏布局
  cat("::::\n\n")
  
  # 全宽图表
  cat("Then something that takes up the entire space\n\n")
  cat("```{r}\n")
  cat("plot(cars)\n")
  cat("```\n\n")
}

# 循环生成n次布局
for (i in 1:n) {
  generate_pattern()
}
### 方案二:纯HTML布局(更稳定)
直接生成HTML标签,避免Pandoc解析的潜在问题,兼容性更好:

```r
---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
n <- 4

generate_html_pattern <- function() {
  # 两栏容器
  cat('<div style="display: flex; gap: 20px; margin-bottom: 30px;">\n')
  
  # 左侧文本区
  cat('  <div style="flex: 1;">\n')
  cat('    <p>Here is the <strong>first</strong> Div.</p>\n')
  cat('    <p>Here is some text that I would like to display on the left</p>\n')
  cat('  </div>\n')
  
  # 右侧图表区
  cat('  <div style="flex: 1;">\n')
  cat('    <p>And this block will be put on the right:</p>\n')
  plot(iris[, -5])
  cat('  </div>\n')
  cat('</div>\n')
  
  # 全宽图表
  cat('<div style="margin-bottom: 30px;">\n')
  cat('  <p>Then something that takes up the entire space</p>\n')
  plot(cars)
  cat('</div>\n')
}

# 循环生成n次
for (i in 1:n) {
  generate_html_pattern()
}
---

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

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

最近更新时间:2026.07.15 11:17:10