英文:
How can I use a loop in RMarkdown to programmatically plot figures and text on individual pages in a document?
问题 {#heading}
以下是翻译好的部分:
以下代码将在一个.Rmd文件中运行,循环遍历一个列表,并使用列表中位置为```i```的元素绘制一个图形。每个图形都绘制在新页面上,因此每个页面只包含一个图形。
请注意,我在三个破折号前添加了撇号,以提高可读性。
---
output: word_document
---
'```{r setup, echo=FALSE, include=FALSE}
library(tidyverse)
make_df <- function(z){
tibble(X = runif(n = 10, min = z, max = z * 2), Y = runif(n = 10, min = z, max = z * 2))
}
'```
'```{r plots, results='asis', warning=FALSE, message=FALSE, echo=FALSE}
newslide <- function(options = "", heading = "", content) {
code <- deparse(substitute(content))
cat(sep = "\n",
knitr::knit_child(
quiet = TRUE,
text = knitr::knit_expand(text = c("{r {{options}} }", "{{code}}", "
"))))
}
for (i in 1:3) {
newslide(
options = paste0("fig.width=6.5, fig.height=8.8, dpi = 600, warning=FALSE, message=FALSE, echo=FALSE"),
content = plot(make_df(i))
)
}
'```
</code></pre>
<p>所有这些都运行良好!然而,<strong>我不仅仅想要图形,我也想要文字</strong> 。我需要一个可以格式化的文本块(例如,我想要能够设置颜色、字体系列、大小等),以便在每个图形下方打印,并且不要看起来像<code>##像这样</code>(我通过在content变量中添加<code>+ cat(&quot;xyz&quot;)</code>来获得了不推荐的格式化)。</p>
<p>文本可以相同,它只是一个将应用于每个图形的标题,但我希望它打印出来并且可以编辑,即不是图形的一部分。
英文:</p>
<p>The code below will run in an .Rmd file to loop through a list and, using the element at position <code>i</code> in the list, plot a figure. Each figure is plotted on a new page, so that each page only contains one figure.</p>
<p><em>Note that I added apostrophes before the triple dash to improve readability here.</em></p>
<pre><code>---
output: word_document
&amp;#39;```{r setup, echo=FALSE, include=FALSE}
library(tidyverse)
make_df &amp;lt;- function(z){
tibble(X = runif(n = 10, min = z, max = z * 2), Y = runif(n = 10, min = z, max = z * 2))
}
&amp;#39;```
&amp;#39;```{r plots, results=&amp;#39;asis&amp;#39;, warning=FALSE, message=FALSE, echo=FALSE}
newslide &amp;lt;- function(options = &amp;quot;&amp;quot;, heading = &amp;quot;&amp;quot;, content) {
code &amp;lt;- deparse(substitute(content))
cat(sep = &amp;quot;\n&amp;quot;,
knitr::knit_child(
quiet = TRUE,
text = knitr::knit_expand(text = c(&amp;quot;{r {{options}} }&amp;amp;quot;, &amp;amp;quot;{{code}}&amp;amp;quot;, &amp;amp;quot;
&amp;quot;))))
}
newslide(
options = paste0(&amp;quot;fig.width=6.5, fig.height=8.8, dpi = 600, warning=FALSE, message=FALSE, echo=FALSE&amp;quot;),
content = plot(make_df(i))
)
}
&amp;#39;```
</code></pre>
<p></code></pre>
<br /></p>
<p>All that works well! However, <strong>I don't just want figures, I want text, too</strong> . 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 <code>## look like this</code> (I was able to get the contraindicated formatting by adding <code>+ cat(&quot;xyz&quot;)</code> in the content variable).</p>
<p>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.</p>
<h1>答案1 {#1}</h1>
<p><strong>得分</strong>: 1</p>
<p>在<code>knit_child()</code>函数的<code>text</code>参数中,你可以像这样添加代码,就好像你在R块内外都在写代码一样:</p>
<pre><code>newslide <- function(options = "", heading = "", content) {
code <- deparse(substitute(content))
<p>cat(sep = "\n",
knitr::knit_child(
quiet = TRUE,
text = c("### Header 3",
"<code>{r results = 'asis'}&quot;, # R块 &quot;#| echo: false&quot;, # R块的选项,如果不使用Quarto,可以删除这一行 code, &quot;</code>",
"",
"文本将出现在图表下方,使用<strong>Markdown</strong>格式化"
)
)
)
}
</code></pre></p>
<p>英文:</p>
<p>In the <code>text</code> argument of <code>knit_child()</code>, you can add code as if you were writing both inside and outside a R chunk, like this:</p>
<pre><code>newslide &lt;- function(options = &quot;&quot;, heading = &quot;&quot;, content) {
code &lt;- deparse(substitute(content))
`cat(sep = &quot;\n&quot;,
knitr::knit_child(
quiet = TRUE,
#text = knitr::knit_expand(text = c(&quot;``{r {{options}} }&amp;quot;, &amp;quot;{{code}}&amp;quot;, &amp;quot;``&quot;))))
text = c(&quot;### Header 3&quot;,
&quot;``{r results = &amp;#39;asis&amp;#39;}&amp;quot;, #the r chunk &amp;quot;#| echo: false&amp;quot;, #options for the chunk, you can delete this if not using Quarto code, &amp;quot;``&quot;,
&quot;&quot;,
&quot;Text that will be below your plot, formatted with `markdown` and all&quot;)
)
)
}
`
</code></pre>
<br />
<br />