有没有办法动态地将pandoc或html代码添加到.RMD文件中?

问题描述 投票:0回答:0

我正在尝试在 RMD 中渲染一个 html 文件,它可以动态生成 pandoc 或 html 代码。具体来说,我希望有两列,左边是文本,右边是绘图,然后是占据整个中间的绘图。我可以很好地做到这一点(参见下面的代码),但我想这样做n次。这是我尝试在顶部复制 n 次的部分,并在底部尝试这样做。请注意,这是一个 .RMD 文件,我从这里获取代码:https://bookdown.org/yihui/rmarkdown-cookbook/multi-column.html

---
output: html_document
---


# Start part I want to reproduce n times

:::: {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:

```{r echo = FALSE}
plot(iris[, -5])
```
:::

::::

Then something that takes up the entire space

```{r}
plot(cars)
```

# End part I want to reproduce n times



# Start my attempt at solving the problem

```{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()
}
```

我也愿意接受使用 html 代码的替代解决方案。谢谢!

r layout r-markdown pandoc bookdown
© www.soinside.com 2019 - 2024. All rights reserved.