首页 > 解决方案 > R没有在Org Babel中输出图表

问题描述

我已经从这个页面尝试了所有四个源代码块: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

尽管图像仍保存为 1.png、2.png 等格式,但它们都没有输出任何内容。

R 肯定是启用的,因为我已经将它用于不需要可视化的其他事情。

标签: rorg-modeorg-babel

解决方案


所以我遇到了这个确切的问题,非常令人沮丧,malcook 的回答基本上解决了它,org-mode在 9.3 更新中有一个重大变化,基本上现在标题必须包含:results output graphics file,而在它file不是必需的之前,一个工作示例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

这应该给出这样的输出:

Org-Babel Ggplot2 示例


推荐阅读