emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Eric Schulte <eric.schulte@gmx.com>
To: Michael Hannon <jm_hannon@yahoo.com>
Cc: Org-Mode List <emacs-orgmode@gnu.org>, "Thomas S. Dye" <tsd@tsdye.com>
Subject: Re: Babel: communicating irregular data to R source-code block
Date: Mon, 23 Apr 2012 17:05:40 -0400	[thread overview]
Message-ID: <87vckqtazf.fsf@gmx.com> (raw)
In-Reply-To: <1335219898.41851.YahooMailNeo@web161901.mail.bf1.yahoo.com> (Michael Hannon's message of "Mon, 23 Apr 2012 15:24:58 -0700 (PDT)")

[-- Attachment #1: Type: text/plain, Size: 516 bytes --]

[...]
>
> 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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-add-fill-header-argument-to-R-code-blocks.patch --]
[-- Type: text/x-patch, Size: 3352 bytes --]

From 45240d367eb981a93f3c694946d4f2a99044cda5 Mon Sep 17 00:00:00 2001
From: Eric Schulte <eric.schulte@gmx.com>
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


[-- Attachment #3: Type: text/plain, Size: 54 bytes --]


Best,

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

  reply	other threads:[~2012-04-23 23:06 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-21 20:17 Babel: communicating irregular data to R source-code block Michael Hannon
2012-04-22  0:44 ` Thomas S. Dye
2012-04-22 15:58   ` Eric Schulte
2012-04-23 16:46     ` Thomas S. Dye
2012-04-23 15:41       ` Eric Schulte
2012-04-23 19:17         ` Thomas S. Dye
2012-04-23 22:24     ` Michael Hannon
2012-04-23 21:05       ` Eric Schulte [this message]
2012-04-24  0:23       ` Thomas S. Dye
2012-04-23 22:55         ` Eric Schulte
2012-04-24  6:44           ` Thomas S. Dye
2012-04-24  7:07             ` Michael Hannon
2012-04-24 17:18               ` Thomas S. Dye
2012-04-24 19:23                 ` Thomas S. Dye
2012-04-25 23:52               ` Thomas S. Dye
2012-04-26  2:06                 ` Michael Hannon
2012-04-26  6:34                   ` Thomas S. Dye

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87vckqtazf.fsf@gmx.com \
    --to=eric.schulte@gmx.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=jm_hannon@yahoo.com \
    --cc=tsd@tsdye.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).