简体   繁体   English

原始HTML和(knitr)循环中的图

[英]Raw HTML and plots in a (knitr) loop

I am putting together a Knitr script to generate a report. 我正在整理一个Knitr脚本来生成报告。 The content of the report is determined dynamically at runtime, where sections of the report are generated via a loop. 报告的内容是在运行时动态确定的,其中报告的各个部分是通过循环生成的。 Ideally I would like to do both of the following in each iteration of the loop: 理想情况下,我希望在循环的每次迭代中都执行以下两项操作:

  1. generate some raw HTML output and 生成一些原始HTML输出并
  2. generate a plot. 生成情节。

However, I am battling to get both of these working at the same time. 但是,我正在努力使这两个功能同时工作。

If I use the following chunk options, then I get the plots but the HTML code is formatted in a "pre" environment. 如果使用以下块选项,则可以得到图,但HTML代码在“预”环境中格式化。

```{r echo=FALSE, fig.keep = "all", fig.show = "asis"}
for (i in 1:3) {
  cat("<h2>Heading</h2>")
  print(ggplot(data.frame(x = 1:10, y = 1:10), aes(x = x, y = y)) + geom_point())
}
```

If I change the chunk options then I get the raw HTML but the plots disappear, being replaced by a short text snippet indicating the path to the plot. 如果更改块选项,则会得到原始的HTML,但是绘图消失了,由指示绘图路径的短文本片段代替。

```{r results="asis", echo=FALSE}
for (i in 1:3) {
  cat("<h2>Heading</h2>")
  print(ggplot(data.frame(x = 1:10, y = 1:10), aes(x = x, y = y)) + geom_point())
}
```

Is it possible to get both working at once? 是否可以同时使它们同时工作?

This is my first attempt to do anything remotely non-trivial with Knitr, so I am sure that there is a simple answer to this question. 这是我第一次尝试对Knitr进行任何微不足道的操作,因此,我相信对此问题有一个简单的答案。

Best regards, Andrew. 最好的问候,安德鲁。

This seems to work at my end: 这似乎在我的工作:

```{r results='asis', echo=F, fig.keep='all'}

for (i in 1:3) {

cat("<h2>Heading</h2>")
pl <- ggplot(data.frame(x = 1:10, y = 1:10), aes(x = x, y = y)) + geom_point()
print(pl)

}```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM