emacs-orgmode
[Top][All Lists]
Advanced

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

Re: [O] Babel: communicating irregular data to R source-code block


From: Eric Schulte
Subject: Re: [O] Babel: communicating irregular data to R source-code block
Date: Mon, 23 Apr 2012 17:05:40 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.1.50 (gnu/linux)

[...]
>
> I.e.,it seems that Org is going to do its own "read.table" before even
> looking at the code in the source block.
>

Yes, this is true, Org will use read.table to read in tabular data.  See
the code in lisp/ob-R.el for specifics.

>
> Is there some way to get Org to use the "fill=TRUE" option on a case-by-case
> basis?
>

Yes, The attached patch allows the :fill header argument to be specified
adding "fill=TRUE" to the read.table function call.  Please try it out
and let me know if it works for you.

>From 45240d367eb981a93f3c694946d4f2a99044cda5 Mon Sep 17 00:00:00 2001
From: Eric Schulte <address@hidden>
Date: Mon, 23 Apr 2012 17:04:37 -0400
Subject: [PATCH] add :fill header argument to R code blocks

* lisp/ob-R.el (org-babel-header-args:R): List this as a viable R
  header argument.
  (org-babel-variable-assignments:R): Check the value of this new
  :fill header argument.
  (org-babel-R-assign-elisp): Set "fill=TRUE" if the :fill header
  argument has been used.
---
 lisp/ob-R.el |   33 +++++++++++++++++++++------------
 1 file changed, 21 insertions(+), 12 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 9538dc4..1427641 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -61,6 +61,7 @@
     (colormodel                 . :any)
     (useDingbats        . :any)
     (horizontal                 . :any)
+    (fill                . ((yes no)))
     (results             . ((file list vector table scalar verbatim)
                            (raw org html latex code pp wrap)
                            (replace silent append prepend)
@@ -148,7 +149,8 @@ This function is called by `org-babel-execute-src-block'."
        (org-babel-R-assign-elisp
        (car pair) (cdr pair)
        (equal "yes" (cdr (assoc :colnames params)))
-       (equal "yes" (cdr (assoc :rownames params)))))
+       (equal "yes" (cdr (assoc :rownames params)))
+       (equal "yes" (cdr (assoc :fill params)))))
      (mapcar
       (lambda (i)
        (cons (car (nth i vars))
@@ -164,19 +166,26 @@ This function is called by `org-babel-execute-src-block'."
       (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
     (format "%S" s)))
 
-(defun org-babel-R-assign-elisp (name value colnames-p rownames-p)
+(defun org-babel-R-assign-elisp (name value colnames-p rownames-p fill-p)
   "Construct R code assigning the elisp VALUE to a variable named NAME."
   (if (listp value)
-      (let ((transition-file (org-babel-temp-file "R-import-")))
-        ;; ensure VALUE has an orgtbl structure (depth of at least 2)
-        (unless (listp (car value)) (setq value (list value)))
-        (with-temp-file transition-file
-          (insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
-          (insert "\n"))
-        (format "%s <- read.table(\"%s\", header=%s, row.names=%s, 
sep=\"\\t\", as.is=TRUE)"
-                name (org-babel-process-file-name transition-file 'noquote)
-               (if (or (eq (nth 1 value) 'hline) colnames-p) "TRUE" "FALSE")
-               (if rownames-p "1" "NULL")))
+      (flet ((R-bool (bool) (if bool "TRUE" "FALSE")))
+       (let ((transition-file (org-babel-temp-file "R-import-")))
+         ;; ensure VALUE has an orgtbl structure (depth of at least 2)
+         (unless (listp (car value)) (setq value (list value)))
+         (with-temp-file transition-file
+           (insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
+           (insert "\n"))
+         (format "%s <- read.table(\"%s\", %s, as.is=TRUE)"
+                 name (org-babel-process-file-name transition-file 'noquote)
+                 (mapconcat (lambda (pair) (concat (car pair) "=" (cdr pair)))
+                            `(("header"    . ,(R-bool (or (eq (nth 1 value)
+                                                              'hline)
+                                                          colnames-p)))
+                              ("row.names" . ,(if rownames-p "1" "NULL"))
+                              ("sep"       . "\"\\t\"")
+                              ("fill"      . ,(R-bool fill-p)))
+                            ", "))))
     (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
 
 (defvar ess-ask-for-ess-directory nil)
-- 
1.7.10

Best,

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

reply via email to

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