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.

Gilly
  • 211
  • 1
  • 5

3 Answers3

6

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

Leu_Grady
  • 2,420
  • 1
  • 17
  • 27
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 |
RUserPassingBy
  • 1,078
  • 7
  • 14
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).

RUserPassingBy
  • 1,078
  • 7
  • 14