Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no output from org-babel code using R

Tags:

emacs

r

org-babel

Org 8.2.10 Emacs 24.5.1 OSX 10.10.3

#+BEGIN_SRC R    
1 + 2
#+END_SRC

#+RESULTS:
: 3

but with

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

#+RESULTS:

"Code block produced no output" and the *Messages* buffer contains:

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

I am not sure why I don't see any output from the second example. It runs find in the R installation on my machine,

I'd be very grateful for any help.

like image 347
user2162871 Avatar asked Jun 28 '15 12:06

user2162871


1 Answers

Add :results output to the header line, e.g.

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

Org 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.

Since your first block returns a regular value it works without specifying anything for :results, using the default value setting.

Your second 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 (emphasis mine):

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.

As mentioned, explicitly calling print and running the code in a :session are other possible solutions.

like image 69
Chris Avatar answered Oct 01 '22 00:10

Chris