使用 R 的 org-babel 代码没有输出

no output from org-babel code using R

组织 8.2.10 Emacs 24.5.1 OSX 10.10.3

#+BEGIN_SRC R    
1 + 2
#+END_SRC

#+RESULTS:
: 3

#+BEGIN_SRC R
  x <- rnorm(100)
  summary(x)
#+END_SRC

#+RESULTS:

"Code block produced no output" *Messages* 缓冲区包含:

Error reading results: (beginning-of-buffer)
Code block produced no output.

我不确定为什么我没有看到第二个示例的任何输出。它在我机器上的 R 安装中运行 find,

如有任何帮助,我将不胜感激。

:results output 添加到 header 行,例如

#+BEGIN_SRC R :results output
  x <- rnorm(100)
  summary(x)
#+END_SRC

#+RESULTS:
:     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
: -2.63500 -0.42370  0.02539  0.04136  0.48370  1.92000 

组织 can capture results from code blocks in two different ways:

The following options are mutually exclusive, and specify how the results should be collected from the code block.

  • value This is the default. The result is the value of the last statement in the code block. This header argument places the evaluation in functional mode. Note that in some languages, e.g., Python, use of this result type requires that a return statement be included in the body of the source code block. E.g., :results value.

  • output The result is the collection of everything printed to STDOUT during the execution of the code block. This header argument places the evaluation in scripting mode. E.g., :results output.

由于您的第一个块 returns 是一个常规值,因此无需为 :results 指定任何内容即可使用默认 value 设置。

你的第二个区块returns a value that must be explicitly printed

The default method returns an object of class c("summaryDefault", "table") which has a specialized print method.

R code that returns values with specialized print methods must be treated specially in Org(强调我的):

If the source code block uses grid-based R graphics, e.g., the lattice and ggplot2 packages, then care must be taken either to print() the graphics object, specify :results output, or run the code in a :session. This is because the graphics functions from lattice and ggplot2 return objects that must be explicitly printed to see them, using the print function. This happens automatically when run interactively, e.g., :session, but when called inside another function, it does not. The way :results value is defined to operate, device and ggplot2 function calls are wrapped in a main function, and unless the object is specifically printed, no output is produced.

如前所述,显式调用 print 和 运行 :session 中的代码是其他可能的解决方案。