emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] [babel] Specified colnames


From: Eric Schulte
Subject: Re: [O] [babel] Specified colnames
Date: Mon, 08 Apr 2013 10:41:44 -0600
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

"Sebastien Vauban" <address@hidden> writes:

> Hi Eric,
>
> I'm trying to specify the column names of the table.
>
> #+name: input
> | title | baz |
> |-------+-----|
> | obs1  | foo |
> | obs2  | bar |
>
> But both versions (with symbols or strings) just do return the plain input
> table.
>
> #+name: R-echo-colnames-list
> #+begin_src R :var data=input :exports results :colnames '(Rev Author)
> data
> #+end_src
>
> #+name: R-echo-colnames-qlist
> #+begin_src R :var data=input :exports results :colnames '("Rev" "Author")
> data
> #+end_src
>
> Am I doing something wrong?
>
> Best regards,
>   Seb

It looks like ob-R implements its own result table reconstruction
instead of using the general support.  This is because R actually has a
notion of column names and row names internally.  The implementation in
ob-R does not correctly handle specified colnmaes as your example shows.

The attached patch brings ob-R closer to the using the unified general
table reconstructed used in most other languages, and fixes your problem
mentioned above.  I haven't applied it however, as it may introduce
other bugs related to specifying column names from within R.  For
example, I'm not sure that it will now correctly apply column names from
a table built entirely from within R.

Additional testing by someone more familiar with R than myself would be
greatly appreciated.

Thanks,

>From 680bfe22c423d5fad43348fc86eba6618f3aa57c Mon Sep 17 00:00:00 2001
From: Eric Schulte <address@hidden>
Date: Mon, 8 Apr 2013 10:36:32 -0600
Subject: [PATCH] use org-babel-reassemble-table in ob-R

* lisp/ob-R.el (org-babel-execute:R): Use org-babel-reassemble-table to
  reconstruct tables with column and row names.
  (org-babel-R-evaluate-external-process): Don't use R-specific table
  reconstruction.
  (org-babel-R-evaluate-session): Don't use R-specific table
  reconstruction.
---
 lisp/ob-R.el | 46 ++++++++++++++++++----------------------------
 1 file changed, 18 insertions(+), 28 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 67d3c37..b424f0f 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -111,13 +111,14 @@ This function is called by `org-babel-execute-src-block'."
           (result
            (org-babel-R-evaluate
             session full-body result-type result-params
-            (or (equal "yes" colnames-p)
-                (org-babel-pick-name
-                 (cdr (assoc :colname-names params)) colnames-p))
-            (or (equal "yes" rownames-p)
-                (org-babel-pick-name
-                 (cdr (assoc :rowname-names params)) rownames-p)))))
-      (if graphics-file nil result))))
+            (equal "yes" colnames-p) (equal "yes" rownames-p))))
+      (if graphics-file nil
+       (org-babel-reassemble-table
+        result
+        (org-babel-pick-name (cdr (assoc :colname-names params))
+                             (cdr (assoc :colnames params)))
+        (org-babel-pick-name (cdr (assoc :rowname-names params))
+                             (cdr (assoc :rownames params))))))))
 
 (defun org-babel-prep-session:R (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."
@@ -301,13 +302,11 @@ last statement in BODY, as elisp."
                                 "FALSE")
                               (format "{function ()\n{\n%s\n}}()" body)
                               (org-babel-process-file-name tmp-file 'noquote)))
-       (org-babel-R-process-value-result
-       (org-babel-result-cond result-params
-         (with-temp-buffer
-           (insert-file-contents tmp-file)
-           (buffer-string))
-         (org-babel-import-elisp-from-file tmp-file '(16)))
-       column-names-p)))
+       (org-babel-result-cond result-params
+        (with-temp-buffer
+          (insert-file-contents tmp-file)
+          (buffer-string))
+        (org-babel-import-elisp-from-file tmp-file '(16)))))
     (output (org-babel-eval org-babel-R-command body))))
 
 (defun org-babel-R-evaluate-session
@@ -333,13 +332,11 @@ last statement in BODY, as elisp."
                    (if row-names-p "NA" "TRUE")
                  "FALSE")
                ".Last.value" (org-babel-process-file-name tmp-file 'noquote)))
-       (org-babel-R-process-value-result
-       (org-babel-result-cond result-params
-         (with-temp-buffer
-           (insert-file-contents tmp-file)
-           (buffer-string))
-         (org-babel-import-elisp-from-file tmp-file '(16)))
-       column-names-p)))
+       (org-babel-result-cond result-params
+        (with-temp-buffer
+          (insert-file-contents tmp-file)
+          (buffer-string))
+        (org-babel-import-elisp-from-file tmp-file '(16)))))
     (output
      (mapconcat
       #'org-babel-chomp
@@ -359,13 +356,6 @@ last statement in BODY, as elisp."
                                    "\n"))
                 (inferior-ess-send-input)))))) "\n"))))
 
-(defun org-babel-R-process-value-result (result column-names-p)
-  "R-specific processing of return value.
-Insert hline if column names in output have been requested."
-  (if column-names-p
-      (cons (car result) (cons 'hline (cdr result)))
-    result))
-
 (provide 'ob-R)
 
 
-- 
1.8.2

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

reply via email to

[Prev in Thread] Current Thread [Next in Thread]