4

How can I get a graph back in my org-babel results for R code?

#+BEGIN_SRC R :results output latex 
boxplot(islands)
#+END_SRC

Islands is a baked in dataset for R.

Do you do it with

#+resname:

and org-auto-load-images = T in your .emacs?

resname example

Perfect Answer

1 Answer 1

6

You're very close! Two changes:

For the R code, you'll need a few more headers:

#+BEGIN_SRC R :results output graphics :file test.png
  library("ggplot2")
  ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
  geom_point()
#+END_SRC

(Note that the headers change slightly depending on whether you want to use base R, as in your example, or something like ggplot2, as in mine. See the docs here for more exposition on this. TL;DR: include output if you want to use ggplot2 et al.)

To display the image, you've got a couple choices:

  • org-toggle-inline-images will switch inline image display on and off
  • org-startup-with-inline-images is a variable which can be set in your config files (init.el / .emacs.d / whathaveyou). If non-nil, images will be shown by default on buffer load.

Also useful is org-redisplay-inline-images, for refreshing an image that's changed on disk if org doesn't auto-reload it.

2
  • Is there a more complicated version if I want the png in the emacs buffer and I will be exporting to html (C-c C-e h o) and pdf (C-c C-e l o) versions? Neither of those has images in the output at the moment. Apr 3, 2016 at 18:30
  • 2
    Without seeing your current code, my hunch is you need to add the header arg :exports both to your src block. By default, only code is exported.
    – Gastove
    Apr 4, 2016 at 3:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.