6

Is there a way to show the results of a Source Block horizontally, or equivalently to transpose an org-table?

For example, I have the following:

#+begin_src R :results table
c('a','b','c')
#+end_src

#+RESULTS:
| a |
| b |
| c |

I have discovered I can fix this by returning a list:

#+begin_src R :results table
as.list(c('a','b','c'))
#+end_src

#+RESULTS:
| a | b | c |

However, it would be preferable if I could do it without changing the source code.

3 Answers 3

3

Here is a try for automating transpose on export, I hope it helps.

Example 1: Basic

First, We name each of R code blocks by placing #+NAME:.

#+NAME: r-out-01
#+begin_src R :results output value
c('a','b','c')
#+end_src

#+RESULTS: r-out-01
| a |
| b |
| c |


#+NAME: r-out-02
#+begin_src R :results output value
matrix(1:8, ncol = 4, byrow = TRUE)
##matrix(1:8, nrow = 4)
#+end_src

#+RESULTS: r-out-02
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |

Next, Emacs Lisp block transpose values from r-out-01.

#+BEGIN_SRC emacs-lisp :results value :var tmp-01=r-out-01
;; (mapcar 'car tmp-01) ;; for a vector
(apply #'mapcar* #'list tmp-01) ;; for both a vector and a matrix
#+END_SRC

#+RESULTS:
| a | b | c |


#+BEGIN_SRC emacs-lisp :results value :var tmp-02=r-out-02
(apply #'mapcar* #'list tmp-02)
#+END_SRC

#+RESULTS:
| 1 | 5 |
| 2 | 6 |
| 3 | 7 |
| 4 | 8 |

So, just #+NAME: R code block, add 3 lines of Emacs Lisp after your R code block.

Example 2: Function

If you have to transpose so often, make it more convienient by #+CALL:. C-c C-c on the line #+CALL: will produce transposed #+RESULTS:.

#+NAME: transpose-r-out
#+BEGIN_SRC emacs-lisp :var tmp="" :results output value :exports code
(if (list tmp)(apply #'mapcar* #'list tmp) nil)
#+END_SRC


#+CALL: transpose-r-out(tmp=r-out-01) :results output value

#+RESULTS:
| a | b | c |


#+CALL: transpose-r-out(tmp=r-out-02) :results output value

#+RESULTS:
| 1 | 5 |
| 2 | 6 |
| 3 | 7 |
| 4 | 8 |
1
  • Awesome, this looks nice and automated
    – Gilly
    Commented Nov 27, 2015 at 2:04
6

There is a function called org-table-transpose-table-at-point which can be used to transpose table at cursor.

2
  • 1
    Great, thanks. Is there a way to automate this on export?
    – Gilly
    Commented Aug 4, 2015 at 22:50
  • 1
    @Gilly yes, just add :post org-table-transpose-table-at-point to the header orgmode.org/manual/post.html#post
    – ibizaman
    Commented Jan 11, 2018 at 21:44
2

Another one here, I updated with nadvice.el.

1. Normal Evaluation

Now, We have normal evaluation and results here.

#+begin_src R :results value
c('a','b','c')
#+end_src

#+RESULTS:
| a |
| b |
| c |

2. Function Override

You can override temporary org-babel-R-process-value-result in ob-R.el by C-c C-c the following code.

#+BEGIN_SRC emacs-lisp :results value
(defun org-babel-R-process-value-result-transpose (result column-names-p)
  (apply #'mapcar* #'list result))

(advice-add 'org-babel-R-process-value-result
            :override #'org-babel-R-process-value-result-transpose)
#+END_SRC

3. Transposed evaluation

Hereafter, every result of R evaluation will be transposed.

#+begin_src R :results value
c('a','b','c')
#+end_src

#+RESULTS:
| a | b | c |

4. Remove Function Override

You have to redefine back by C-c C-c the following code for normal org-mode operation.

#+BEGIN_SRC emacs-lisp :results value
(advice-remove 'org-babel-R-process-value-result
               #'org-babel-R-process-value-result-transpose)
#+END_SRC

5. Normal Evaluation

Results are in normal operation now.

#+begin_src R :results value
c('a','b','c')
#+end_src

#+RESULTS:
| a |
| b |
| c |

It will be useful if you evaluate whole org-mode document by C-c C-v C-b (M-x org-babel-execute-buffer).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.