如何生成绘图并将其显示在 rnw 文件的其他位置?

How to generate a plot and display it elsewhere in rnw file?

我看到了这个 https://bookdown.org/yihui/rmarkdown-cookbook/fig-chunk.html 我想知道如果使用我用 knitr 编译成 pdf 的 .rnw 文件是否有类似的东西。 在此先感谢您的帮助。

是的,您可以使用相同的技巧。您需要做的就是在您的文档中使用正确的语法,即在您的情况下使用 LaTeX。以下是您提到的示例从 (R) Markdown 到 Rnw/LaTeX:

的翻译
We generate a plot in this code chunk but do not show it:

<<cars-plot, dev='pdf', fig.show='hide'>>=
plot(cars)
@

After another paragraph, we introduce the plot:

\includegraphics{\Sexpr{knitr::fig_chunk('cars-plot', 'pdf')}}