Outputting figure from R to LaTeX in source block with caption

I am trying to run a source code block and have the outputted figure go into a LaTeX document as a figure. Basically I am doing this in a way slightly similar to Jupyter notebooks, but with the ability to work in org and have access to LaTeX export.

I have already come across this answer which does an example for Python, which does not seem to resolve my issue and I still have a problem with the Python version of this answer.

My MWE for the org file is:

* Testing source

#+NAME: fig:20170117
#+BEGIN_SRC R  :file test.png :session org_org :results graphics
x <- c(0, 3, 8, 9, 0)
y <- c(1, 4, 5, 6, 7)
plot.new()
plot(x, y, xlab="test_x", ylab="test_y")
mtext("Some plot")
#+END_SRC
#+CAPTION: Output caption
#+LABEL: fig:20170117
#+ATTR_LATEX: :width 0.75\textwidth
#+RESULTS: fig:20170117
[[file:test.png]]

#+NAME: fig:org_fig
#+BEGIN_SRC python :file org.png  :session org_py :results graphics
clf()
plot(np.sin(np.arange(0,7,0.1)))
plt.savefig('org.png', transparent=True, bbox_inches='tight')
'org.png'
#+END_SRC
#+CAPTION: Org Fig
#+LABEL: fig:org_fig
#+ATTR_LATEX: :width 0.5\textwidth
#+RESULTS: fig:org_fig
[[file:org.png]]


And exporting both of these gives the LaTeX body with the code but not the correct labels or either of the figures. If I comment out the #+RESULTS: line I will get the figures, but without the captions and labels.:

\section{Testing source}
\label{sec:orge034bc3}

\lstset{language=r,label=org47910a7,caption= ,captionpos=b,numbers=none}
\begin{lstlisting}
x <- c(0, 3, 8, 9, 0)
y <- c(1, 4, 5, 6, 7)
plot.new()
plot(x, y, xlab="test_x", ylab="test_y")
mtext("Some plot")
\end{lstlisting}
\lstset{language=Python,label=org677e653,caption= ,captionpos=b,numbers=none}
\begin{lstlisting}
clf()
plot(np.sin(np.arange(0,7,0.1)))
plt.savefig('org.png', transparent=True, bbox_inches='tight')
'org.png'
\end{lstlisting}


3 Answers

By default, org exports code but not the results of the code. You need to ask it to do both with the :exports argument. You can then add the caption before the #+RESULTS: tag:

* Testing source

#+NAME: fig:20170117
#+BEGIN_SRC R  :file test.png :session org_org :exports both
x <- c(0, 3, 8, 9, 0)
y <- c(1, 4, 5, 6, 7)
plot(x, y, xlab="test_x", ylab="test_y")
mtext("Some plot")
#+END_SRC

#+CAPTION: My first figure
#+RESULTS: fig:20170117
[[file:test.png]]

#+NAME: fig:org_fig
#+BEGIN_SRC python :file org.png  :session org_py :exports both
clf()
plot(np.sin(np.arange(0,7,0.1)))
plt.savefig('org.png', transparent=True, bbox_inches='tight')
'org.png'
#+END_SRC

#+CAPTION: My second figure
#+RESULTS: fig:org_fig
[[file:org.png]]


Exporting this to LaTeX produces:

...
\section*{Testing source}
\label{sec:org1a89d52}

\lstset{backgroundcolor=\color[gray]{0.95},keywordstyle=\color{blue},frame=single,commentstyle=\color{BrickRed},columns=fixed,basicstyle=\ttfamily,stringstyle=\color{Red},keepspaces=true,showstringspaces=false,deletekeywords={ps,scan},language=R,label=org129c1a7,caption= ,captionpos=b,numbers=none}
\begin{lstlisting}
x <- c(0, 3, 8, 9, 0)
y <- c(1, 4, 5, 6, 7)
## plot.new() not needed!
plot(x, y, xlab="test_x", ylab="test_y")
mtext("Some plot")
\end{lstlisting}

\begin{figure}[htbp]
\centering
\includegraphics[width=.9\linewidth]{test.png}
\caption{My first figure}
\end{figure}

...

• This works if I put :results graphics on the #+BEGIN_SRC line, but otherwise works as expected. – chasely Jan 31 '17 at 18:51

You can put the caption above the results like below:

#+CAPTION: bLa #+RESULTS: [[file:tmp.png]]

But first you have to create the result tag, evaluate the block and then evaluate the whole construct. (I'm on org 9.0.4)

Additionaly, I was having problems where the figure would not be exported even with the :exports both tag. I fixed this problem by installing the R package "tikzDevice" and add this to ~/.Rprofile:

old <- getOption("defaultPackages")

options(defaultPackages = c(old,"tikzDevice"))

Note that I had this problem in MacOS and not sure if this is required in Linux or Windows.