1

I'm using org mode to write some R code. Somehow, the output I get inside my org file with C-c C-c differ from what I get when I run the same code from the command line. Consider for example the following excerpt:

#+begin_src R
  month_no <- 1:12
  attr(month_no, "names") <- month.name
  month_no
#+end_src

The result in org-mode looks like

#+RESULTS:
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
| 11 |
| 12 |

When run as a script from the command line (using Rscript.exe), the output is

  January  February     March     April       May      June      July    August
        1         2         3         4         5         6         7         8
September   October  November  December
        9        10        11        12

The same happens when I use the interactive R command line (R.exe). So I get that org mode tries to format the result as a table, but it also omits the names. Is there a way to get the same output as from the command line, or at least preserve the names? I took a look at the Results of Evaluation documentation, but even :results value verbatim does omit the names, and just gives a list of values.

3
  • I don't know much about R, but I think this is (mostly) the difference of :results value and :results output. Try the latter but you will have to change the last line of the script to print(month_no) to get some output that Org Babel can display. Commented yesterday
  • Actually, you don't have to change the last line: month_no produces the expanded form with :results output. Commented yesterday
  • @NickD Indeed, :results output works, not sure why I didn't try this before... If you write it as an answer I'd be happy to accept :) Commented 16 hours ago

1 Answer 1

0

The way to see the output of the R process on the script is to specify :results output, instead of the default :results value:

#+begin_src R :results output drawer
month_no <- 1:12
attr(month_no, "names") <- month.name
month_no
#+end_src

#+RESULTS:
:results:
  January  February     March     April       May      June      July    August 
        1         2         3         4         5         6         7         8 
September   October  November  December 
        9        10        11        12 
:end:

With :results output, the evaluation of the block consists of running an R command (in my case (Linux), the command is R --slave --no-save: it's the value of the variable org-babel-R-command which can be customized if necessary), and collecting the standard output of the process.

With :results value the processing is more complicated: 16 lines of code vs. 1 line for the output case.

See the function org-babel-R-evaluate-external-process: in the output case, the processing just runs the R interpreter on the code of the block and gathers up the standard output as the return value (a string); in the value case, it creates some R code on the fly (see the value of org-babel-R-write-object-command) which wraps the block in an R function and calls that function. It then formats the value that is returned and writes it into a temp file. That file is munged by org-babel-R-process-value-result and turns it into a table in Lisp format (in the case above, that's a list of 12 singleton lists: ((1) (2) (3) ... (12)) ) which is then turned into whatever format you specify in the header args (by default, that's an Org table).

These gory details are more-or-less uniform across Babel languages, but there are corner cases which might rear their ugly heads and suprise you. The only way to deal with them is to read the language-specific Babel documentation (see Language Support in Worg - the R documentation can be found here). You should also read the commentary at the top of the ob-<language>.el file - for some languages, but not R, that's the only documentation. For the R case, the commentary can be found here. And eventually, you should consult the code (as I did for this answer).

You should also experiment assiduously: write small blocks, change header arguments and run the examples; ultimately, it's the only sure-fire way to find out what happens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.