Home > Back-end >  How to generate a plot and display it elsewhere in rnw file?
How to generate a plot and display it elsewhere in rnw file?

Time:03-20

I saw this https://bookdown.org/yihui/rmarkdown-cookbook/fig-chunk.html and I'm wondering if there is something similar if a use a .rnw file that I compile in a pdf with knitr. Thanks in advance for the help.

CodePudding user response:

Yes, you can use the same trick. All you need to do is use the right syntax in your document, i.e., LaTeX in your case. Below is a translation of the example you mentioned from (R) Markdown to 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')}}
  • Related