How can I use a loop in RMarkdown to programmatically plot figures and text on individual pages in a document?

huangapple go评论153阅读模式
英文:

How can I use a loop in RMarkdown to programmatically plot figures and text on individual pages in a document?

问题

以下是翻译好的部分:

  1. 以下代码将在一个.Rmd文件中运行,循环遍历一个列表,并使用列表中位置为```i```的元素绘制一个图形。每个图形都绘制在新页面上,因此每个页面只包含一个图形。
  2. *请注意,我在三个破折号前添加了撇号,以提高可读性。*
  3. ```markdown
  4. ---
  5. output: word_document
  6. ---
  7. '```{r setup, echo=FALSE, include=FALSE}
  8. library(tidyverse)
  9. make_df <- function(z){
  10. tibble(X = runif(n = 10, min = z, max = z * 2), Y = runif(n = 10, min = z, max = z * 2))
  11. }
  12. '```
  13. '```{r plots, results='asis', warning=FALSE, message=FALSE, echo=FALSE}
  14. newslide <- function(options = "", heading = "", content) {
  15. code <- deparse(substitute(content))
  16. cat(sep = "\n",
  17. knitr::knit_child(
  18. quiet = TRUE,
  19. text = knitr::knit_expand(text = c("```{r {{options}} }", "{{code}}", "```"))))
  20. }
  21. for (i in 1:3) {
  22. newslide(
  23. options = paste0("fig.width=6.5, fig.height=8.8, dpi = 600, warning=FALSE, message=FALSE, echo=FALSE"),
  24. content = plot(make_df(i))
  25. )
  26. }
  27. '```

所有这些都运行良好!然而,我不仅仅想要图形,我也想要文字。我需要一个可以格式化的文本块(例如,我想要能够设置颜色、字体系列、大小等),以便在每个图形下方打印,并且不要看起来像##像这样(我通过在content变量中添加+ cat("xyz")来获得了不推荐的格式化)。

文本可以相同,它只是一个将应用于每个图形的标题,但我希望它打印出来并且可以编辑,即不是图形的一部分。

英文:

The code below will run in an .Rmd file to loop through a list and, using the element at position i in the list, plot a figure. Each figure is plotted on a new page, so that each page only contains one figure.

Note that I added apostrophes before the triple dash to improve readability here.

  1. ---
  2. output: word_document
  3. ---
  4. '```{r setup, echo=FALSE, include=FALSE}
  5. library(tidyverse)
  6. make_df <- function(z){
  7. tibble(X = runif(n = 10, min = z, max = z * 2), Y = runif(n = 10, min = z, max = z * 2))
  8. }
  9. '```
  10. '```{r plots, results='asis', warning=FALSE, message=FALSE, echo=FALSE}
  11. newslide <- function(options = "", heading = "", content) {
  12. code <- deparse(substitute(content))
  13. cat(sep = "\n",
  14. knitr::knit_child(
  15. quiet = TRUE,
  16. text = knitr::knit_expand(text = c("```{r {{options}} }", "{{code}}", "```"))))
  17. }
  18. for (i in 1:3) {
  19. newslide(
  20. options = paste0("fig.width=6.5, fig.height=8.8, dpi = 600, warning=FALSE, message=FALSE, echo=FALSE"),
  21. content = plot(make_df(i))
  22. )
  23. }
  24. '```

All that works well! However, I don't just want figures, I want text, too. I need a block of nicely formattable text (eg I want to be able to set the color, font family, size, etc) to print below each figure and for it not to ## look like this (I was able to get the contraindicated formatting by adding + cat("xyz") in the content variable).

The text can be identical, it's just a caption that will apply to each figure, but I do want it to print and be editable, ie not part of the plot.

答案1

得分: 1

knit_child()函数的text参数中,你可以像这样添加代码,就好像你在R块内外都在写代码一样:

  1. newslide <- function(options = "", heading = "", content) {
  2. code <- deparse(substitute(content))
  3. cat(sep = "\n",
  4. knitr::knit_child(
  5. quiet = TRUE,
  6. text = c("### Header 3",
  7. "```{r results = 'asis'}", # R块
  8. "#| echo: false", # R块的选项,如果不使用Quarto,可以删除这一行
  9. code,
  10. "```",
  11. "",
  12. "文本将出现在图表下方,使用**Markdown**格式化"
  13. )
  14. )
  15. )
  16. }
英文:

In the text argument of knit_child(), you can add code as if you were writing both inside and outside a R chunk, like this:

  1. newslide &lt;- function(options = &quot;&quot;, heading = &quot;&quot;, content) {
  2. code &lt;- deparse(substitute(content))
  3. cat(sep = &quot;\n&quot;,
  4. knitr::knit_child(
  5. quiet = TRUE,
  6. #text = knitr::knit_expand(text = c(&quot;```{r {{options}} }&quot;, &quot;{{code}}&quot;, &quot;```&quot;))))
  7. text = c(&quot;### Header 3&quot;,
  8. &quot;```{r results = &#39;asis&#39;}&quot;, #the r chunk
  9. &quot;#| echo: false&quot;, #options for the chunk, you can delete this if not using Quarto
  10. code,
  11. &quot;```&quot;,
  12. &quot;&quot;,
  13. &quot;Text that will be below your plot, formatted with **markdown** and all&quot;)
  14. )
  15. )
  16. }

huangapple
  • 本文由 发表于 2023年8月11日 01:40:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定