简体   繁体   English

从循环中打印 RMarkdown 标题

[英]Print RMarkdown captions from a loop

I am creating a series of plots from within a loop in an RMarkdown document, then knitting this to a PDF.我正在从 RMarkdown 文档的循环中创建一系列图,然后将其编织成 PDF。 I can do this without any problem, but I would like the caption to reflect the change between each plot.我可以毫无问题地做到这一点,但我希望标题能够反映每个情节之间的变化。 A MWE is shown below: MWE 如下所示:

---
title: "Caption loop"
output: pdf_document
---

```{r, echo=FALSE}
library(tidyverse)

p <- 
  map(names(mtcars), ~ggplot(mtcars) +
      geom_point(aes_string(x = 'mpg', y = .))) %>% 
  set_names(names(mtcars))
```

```{r loops, fig.cap=paste(for(i in seq_along(p)) print(names(p)[[i]])), echo=FALSE}
for(i in seq_along(p)) p[[i]] %>% print
```

I have made a first attempt at capturing the plots and storing in a variable p , and trying to use that to generate the captions, but this isn't working.我第一次尝试捕获绘图并将其存储在变量p中,并尝试使用它来生成标题,但这不起作用。 I haven't found too much about this on SO, despite this surely being something many people would need to do.尽管这肯定是许多人需要做的事情,但我在 SO 上没有找到太多关于此的内容。 I did find this question , but it looks so complicated that I was wondering if there is a clear and simple solution that I am missing.我确实找到了这个问题,但它看起来很复杂,以至于我想知道是否有一个我缺少的清晰简单的解决方案。

I wondered if it has something to do with eval.after , as with this question , but that does not involve plots generated within a loop.我想知道它是否与eval.after ,就像这个问题一样,但这不涉及在循环中生成的图。

many thanks for your help!非常感谢您的帮助!

It seems that knitr is smart enough to do the task automatically.似乎knitr足够聪明,可以自动完成任务。 By adding names(mtcars) to the figure caption, knitr iterates through these in turn to produce the correct caption.通过将names(mtcars)添加到图形标题,knitr 依次迭代这些以生成正确的标题。 The only problem now is how to stop all of the list indexes from printing in the document...现在唯一的问题是如何阻止所有列表索引在文档中打印......

---
title: "Caption loop"
output: pdf_document
---

```{r loops, fig.cap=paste("Graph of mpg vs.", names(mtcars)), message=FALSE, echo=FALSE, warning=FALSE}
library(tidyverse)

map(
  names(mtcars),
  ~ ggplot(mtcars) +
    geom_point(aes_string(x = 'mpg', y = .))
)
```

In case this might be useful to somebody.以防这可能对某人有用。 Here is an adaptation of Jonny's solution for captions without printing list indices.这是 Jonny 对不打印列表索引的字幕解决方案的改编。 This can be achieved by using purrr::walk instead of purrr::map.这可以通过使用 purrr::walk 而不是 purrr::map 来实现。 Also included is a latex fig label and text that references each plot.还包括一个乳胶无花果标签和引用每个图的文本。

---
title: "Loop figures with captions"
output: 
    pdf_document
---


```{r loops, fig.cap=paste(sprintf("\\label{%s}", names(mtcars)), "Graph of mpg vs.", names(mtcars)),results='asis', message=FALSE, echo=FALSE, warning=FALSE}
library(tidyverse)
library(stringi)
walk(names(mtcars),
     ~{
         p <- ggplot(mtcars) +
             geom_point(aes_string(x = 'mpg', y = .))
         #print plot
         cat('\n\n') 
         print(p)
         #print text with refernce to plot
         cat('\n\n') 
         cat(sprintf("Figure \\ref{%s} is a Graph of mpg vs. %s. %s \n\n", ., ., 
                     stri_rand_lipsum(1)))
         cat("\\clearpage")
     })
```

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

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