Attached please find =the reworked patch. 

1) uses local() and closes connection
2) does not leave a variable cal;led file behind

Cheers,

Rainer
From 2989c5890736099521a077d3b15d2ccc37c88b67 Mon Sep 17 00:00:00 2001
From: "Rainer M. Krug" <r.m.k...@gmail.com>
Date: Fri, 20 Jun 2014 22:19:59 +0200
Subject: [PATCH] lisp/ob-R.el: Fix tangling with tables

* lisp/ob-R.el (org-babel-R-assign-elisp): Fix variable transfer of
tables by using text connections in R instead of files.  Variable
transfer of tables does not depend on files anymore, i.e. works also
when tangling.
---
 lisp/ob-R.el | 42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index a3ae1ec..7f366c4 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -190,32 +190,34 @@ This function is called by `org-babel-execute-src-block'."
   (if (listp value)
       (let* ((lengths (mapcar 'length (org-remove-if-not 'sequencep value)))
 	     (max (if lengths (apply 'max lengths) 0))
-	     (min (if lengths (apply 'min lengths) 0))
-	     (transition-file (org-babel-temp-file "R-import-")))
+	     (min (if lengths (apply 'min lengths) 0)))
         ;; 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))
-	   "\n"))
-	(let ((file (org-babel-process-file-name transition-file 'noquote))
+	(let ((file (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
 	      (header (if (or (eq (nth 1 value) 'hline) colnames-p)
 			  "TRUE" "FALSE"))
 	      (row-names (if rownames-p "1" "NULL")))
 	  (if (= max min)
-	      (format "%s <- read.table(\"%s\",
-                      header=%s,
-                      row.names=%s,
-                      sep=\"\\t\",
-                      as.is=TRUE)" name file header row-names)
-	    (format "%s <- read.table(\"%s\",
-                   header=%s,
-                   row.names=%s,
-                   sep=\"\\t\",
-                   as.is=TRUE,
-                   fill=TRUE,
-                   col.names = paste(\"V\", seq_len(%d), sep =\"\"))"
-		    name file header row-names max))))
+	      (format "%s <- local({
+                        con <- textConnection(%S)
+                        res <- read.table(con,
+                          header    = %s,
+                          row.names = %s,
+                          sep       = \"\\t\",
+                          as.is     = TRUE)
+                        close(con)
+                        res})" name file header row-names)
+	    (format "%s <- local({
+                       con <- textConnection(%S)
+                       res <- read.table(con,
+                         header    = %s,
+                         row.names = %s,
+                         sep       = \"\\t\",
+                         as.is     = TRUE,
+                         fill      = TRUE,
+                         col.names = paste(\"V\", seq_len(%d), sep =\"\"))
+                       close(con)
+                       res})" name file header row-names max))))
     (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
 
 (defvar ess-ask-for-ess-directory) ; dynamically scoped
-- 
2.0.0



Rainer M Krug <rai...@krugs.de> writes:

> Charles Berry <ccbe...@ucsd.edu> writes:
>
>> Aaron Ecay <aaronecay <at> gmail.com> writes:
>>
>>> 
>>> Hi Rainer,
>>> 
>>> I have not tested the patch (I rarely use :var), but here are some
>>> comments from reading the patch.
>>> 
>> [snip]
>>> 
>>> >         (header (if (or (eq (nth 1 value) 'hline) colnames-p)
>>> >                     "TRUE" "FALSE"))
>>> >         (row-names (if rownames-p "1" "NULL")))
>>> >     (if (= max min)
>>> > -       (format "%s <- read.table(\"%s\",
>>> > +       (format "%s <- read.table(textConnection('%s'),
>>> 
>>> This will fail if ‘file’ contains an apostrophe character.  I guess you
>>> need to escape apostrophes in ‘file’.
>>> 
>>
>> There are a bunch of issues with the textConnection - the point Aaron
>> makes, the nasty name (try showConnections() to see), and close()-ing it.
>>
>> I think you need something more like this:
>>
>> ====
>>                  (header (if (or (eq (nth 1 value) 'hline) colnames-p)
>>                               "TRUE" "FALSE"))
>>                   (row-names (if rownames-p "1" "NULL")))
>>               (if (= max min)
>>                   (format "
>>     %s <- 
>>         local({con.object <- 
>>     %S
>>     con <- textConnection(con.object)
>>     res <- read.table(con,
>>                       header=%s,
>>                       row.names=%s,
>>                       sep=\"\\t\",
>>                       as.is=TRUE)
>>     close(con)
>>     res})" 
>>                   name file header row-names)
>>
>> ====
>>
>> Notes: %S handles Aaron's quoting issue.
>>        local() keeps unwanted objects out of user's way.
>>        This worked on a couple of simple tests
>
> I agree completely here - thanks for the %S (didn't know about it - now
> I do) and haven't thought about the local() - I think it is a very good
> idea to use it in the context of the variable transfer.
>
>>
>> If you have `:session :results output' headers your session
>> transcript will have `file' dumped into it, which might be an annoyance.
>
> Not only annoyance - can cause overwriting of the data. Will look into
> this.



>
> Thanks a lot,
>
> Rainer
>
>>
>> HTH,
>>
>> Chuck
>>
>>
>>
>>

-- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, 
UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      rai...@krugs.de

Skype:      RMkrug

PGP: 0x0F52F982

Attachment: pgpVsaq6SD16N.pgp
Description: PGP signature

Reply via email to