Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Not Outputting Graphs in Org Babel

I've tried all four of these source blocks from this page: https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-R.html

* does /not/ produce a file
#+begin_src R :file 1.png :results value graphics
library(lattice)
xyplot(1:10 ~ 1:10)
#+end_src
* does produce a file, by printing object
#+begin_src R :file 2.png :results value graphics
library(lattice)
print(xyplot(1:10 ~ 1:10))
#+end_src
* does produce a file, by using :results output
#+begin_src R :file 3.png :results output graphics
library(lattice)
xyplot(1:10 ~ 1:10)
#+end_src
* does produce a file, by evaluating in :session
#+begin_src R :file 4.png :session :results graphics
library(lattice)
xyplot(1:10 ~ 1:10)
#+end_src

All of them just output nothing though the image is still saved as 1.png, 2.png etc.

R is definitely enabled as I've used it for other things that don't require visualizations.

like image 539
Blytheway Avatar asked Jan 25 '23 12:01

Blytheway


2 Answers

So I was having this exact issue and it was extremely frustrating, malcook's answer basically solved it, there is a breaking change to org-mode in the 9.3 update, essentially now the header must include :results output graphics file whereas before it was file was not required, a working example in org-mode 9.3.2:

#+BEGIN_SRC R :exports both :results output graphics file :file Example9832.png
  library(tidyverse)
  mtcars <- as_tibble(mtcars)
  myplot <-  ggplot(mtcars, aes(x = disp, y = mpg, col = hp, shape = as.factor(cyl))) +
    geom_point() +
    theme_classic() +
    labs(col = "HorsePower", shape = "Cylinders")
  myplot
#+END_SRC

And that should give an ouput like this:

Org-Babel Ggplot2 example

like image 111
Ryan Greenup Avatar answered Feb 08 '23 04:02

Ryan Greenup


Please run meta-x org-version.

Is it 9.3?

Then see: Version 9.3 Incompatible changes

:file header argument no longer assume "file" :results The "file" :results value is now mandatory for a code block returning a link to a file. The :file or :file-ext header arguments no longer imply a "file" result is expected.

like image 37
malcook Avatar answered Feb 08 '23 02:02

malcook