title: How to run r code within the latex code in Rmarkdown
body: <p>Consider the following code in <code>Rmarkdown</code>, which produces a graph as below:</p>
  <pre><code>\begin{figure}
  \centering
  \captionsetup{skip=0pt}
  \includegraphics[width=6cm]{example-image-a}
  \caption{A test caption}
  \end{figure}
  </code></pre>
  <p>Output:</p>
  <p><a href="https://i.stack.imgur.com/vQOpK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vQOpK.png" alt="enter image description here" /></a></p>
  <p>Now, instead of just using a graph, I would like to create my own plot in <code>Rmarkdown</code>, for example, using this code:</p>
  <pre><code>\begin{figure}
  \centering
  \captionsetup{skip=0pt}
  \includegraphics[width=6cm]{plot(mtcars$mpg)}
  \caption{A test caption}
  \end{figure}
  </code></pre>
  <p>but the following error is produced instead:</p>
  <pre><code>! LaTeX Error: File `plot(mtcars$mpg)' not found.
  </code></pre>
  <p>Is there any way to run r codes within latex?</p>