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

标签 r emacs org-babel

组织 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:

“代码块没有输出”
并且 *Messages* 缓冲区包含:
Error reading results: (beginning-of-buffer)
Code block produced no output.

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

我将非常感谢任何帮助。

最佳答案

添加 :results output到标题行,例如

#+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.


由于您的第一个 block 返回一个常规值,它无需为 :results 指定任何内容即可工作。 , 使用默认 value环境。
你的第二 block 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 中运行代码是其他可能的解决方案。

关于r - 使用 R 的 org-babel 代码没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31099977/

相关文章:

emacs - 字节码 : Symbol's value as variable is void: org-babel-tangle-lang-exts

r - 检查选择的列是否具有相同的值

typescript - 如何将 tsconfig.json 应用于 Emacs org-babel?

r - 如何使用不同(但固定)均值最大化联合似然函数

emacs - 在 Emacs 中使用 Kawa

emacs - 暂时覆盖编译模式 regexp alist : dynamic binding weirdness?

emacs - 在Emacs中,如何使用Tramp SSH作为其他组ID进行编辑?

emacs - 组织模式源 block 可见性循环

r - 将条目列添加到 r Shiny 数据表的按钮

r - ggplot2:不同美学的传说