emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] -for review- Tangling with variable transfer of variables
@ 2014-06-19 20:19 Rainer M Krug
  2014-06-19 21:09 ` Aaron Ecay
  0 siblings, 1 reply; 16+ messages in thread
From: Rainer M Krug @ 2014-06-19 20:19 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: Eric Schulte


[-- Attachment #1.1: Type: text/plain, Size: 287 bytes --]

Hi

This patch should fix the problem of tangling with variable transfer of
tables. It uses a textConection() instead of a file to transfer the
table. This results in variable transfer of variables in R, without
having to include additional files.

Hope this does not work only for me.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: Patch --]
[-- Type: text/x-patch, Size: 2106 bytes --]

From f77e982e17909f2098974356c304bd29db04da79 Mon Sep 17 00:00:00 2001
From: "Rainer M. Krug" <R.M.Krug@gmail.com>
Date: Thu, 19 Jun 2014 22:03:46 +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 | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index a3ae1ec..88f65f3 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -190,25 +190,20 @@ 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\",
+	      (format "%s <- read.table(textConnection('%s'),
                       header=%s,
                       row.names=%s,
                       sep=\"\\t\",
                       as.is=TRUE)" name file header row-names)
-	    (format "%s <- read.table(\"%s\",
+	    (format "%s <- read.table(textConnection('%s'),
                    header=%s,
                    row.names=%s,
                    sep=\"\\t\",
-- 
2.0.0


[-- Attachment #1.3: Type: text/plain, Size: 442 bytes --]


Cheers,

Rainer


-- 
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:      Rainer@krugs.de

Skype:      RMkrug

PGP: 0x0F52F982

[-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-19 20:19 [PATCH] -for review- Tangling with variable transfer of variables Rainer M Krug
@ 2014-06-19 21:09 ` Aaron Ecay
  2014-06-19 23:23   ` Charles Berry
  2014-06-20 13:07   ` Rainer M Krug
  0 siblings, 2 replies; 16+ messages in thread
From: Aaron Ecay @ 2014-06-19 21:09 UTC (permalink / raw)
  To: Rainer M Krug, emacs-orgmode; +Cc: Eric Schulte

Hi Rainer,

I have not tested the patch (I rarely use :var), but here are some
comments from reading the patch.

2014ko ekainak 19an, Rainer M Krug-ek idatzi zuen:
> 
> Hi
> 
> This patch should fix the problem of tangling with variable transfer of
> tables. It uses a textConection() instead of a file to transfer the
> table. This results in variable transfer of variables in R, without
> having to include additional files.
> 
> Hope this does not work only for me.
> 
> From f77e982e17909f2098974356c304bd29db04da79 Mon Sep 17 00:00:00 2001
> From: "Rainer M. Krug" <R.M.Krug@gmail.com>
> Date: Thu, 19 Jun 2014 22:03:46 +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.

The changelog comment should not be indented on the subsequent lines.
I believe the standard is also to double-space after sentence-ending
periods.

> ---
>  lisp/ob-R.el | 13 ++++---------
>  1 file changed, 4 insertions(+), 9 deletions(-)
> 
> diff --git a/lisp/ob-R.el b/lisp/ob-R.el
> index a3ae1ec..88f65f3 100644
> --- a/lisp/ob-R.el
> +++ b/lisp/ob-R.el
> @@ -190,25 +190,20 @@ 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)))

As a cosmetic issue, you might want to pick a different name for this
variable now that it no longer holds a file name.

>  	      (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’.

HTH,

-- 
Aaron Ecay

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-19 21:09 ` Aaron Ecay
@ 2014-06-19 23:23   ` Charles Berry
  2014-06-20 13:15     ` Rainer M Krug
  2014-06-20 13:07   ` Rainer M Krug
  1 sibling, 1 reply; 16+ messages in thread
From: Charles Berry @ 2014-06-19 23:23 UTC (permalink / raw)
  To: emacs-orgmode

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

If you have `:session :results output' headers your session
transcript will have `file' dumped into it, which might be an annoyance.

HTH,

Chuck

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-19 21:09 ` Aaron Ecay
  2014-06-19 23:23   ` Charles Berry
@ 2014-06-20 13:07   ` Rainer M Krug
  1 sibling, 0 replies; 16+ messages in thread
From: Rainer M Krug @ 2014-06-20 13:07 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: Eric Schulte

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

Aaron Ecay <aaronecay@gmail.com> writes:

> Hi Rainer,
>
> I have not tested the patch (I rarely use :var), but here are some
> comments from reading the patch.
>
> 2014ko ekainak 19an, Rainer M Krug-ek idatzi zuen:
>> 
>> Hi
>> 
>> This patch should fix the problem of tangling with variable transfer of
>> tables. It uses a textConection() instead of a file to transfer the
>> table. This results in variable transfer of variables in R, without
>> having to include additional files.
>> 
>> Hope this does not work only for me.
>> 
>> From f77e982e17909f2098974356c304bd29db04da79 Mon Sep 17 00:00:00 2001
>> From: "Rainer M. Krug" <R.M.Krug@gmail.com>
>> Date: Thu, 19 Jun 2014 22:03:46 +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.
>
> The changelog comment should not be indented on the subsequent lines.
> I believe the standard is also to double-space after sentence-ending
> periods.

OK - I'll fix this.

>
>> ---
>>  lisp/ob-R.el | 13 ++++---------
>>  1 file changed, 4 insertions(+), 9 deletions(-)
>> 
>> diff --git a/lisp/ob-R.el b/lisp/ob-R.el
>> index a3ae1ec..88f65f3 100644
>> --- a/lisp/ob-R.el
>> +++ b/lisp/ob-R.el
>> @@ -190,25 +190,20 @@ 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)))
>
> As a cosmetic issue, you might want to pick a different name for this
> variable now that it no longer holds a file name.

Well - I thought about it, but it holds the value which is passed to the
argument "file" so I left it as it is to make this clear.

>
>>  	      (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’.

True - I did not think to much into the escaping issue. 

Thanks,  

Rainer

>
> HTH,

-- 
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:      Rainer@krugs.de

Skype:      RMkrug

PGP: 0x0F52F982

[-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-19 23:23   ` Charles Berry
@ 2014-06-20 13:15     ` Rainer M Krug
  2014-06-20 20:34       ` Rainer M Krug
  0 siblings, 1 reply; 16+ messages in thread
From: Rainer M Krug @ 2014-06-20 13:15 UTC (permalink / raw)
  To: Charles Berry; +Cc: emacs-orgmode

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

Charles Berry <ccberry@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:      Rainer@krugs.de

Skype:      RMkrug

PGP: 0x0F52F982

[-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-20 13:15     ` Rainer M Krug
@ 2014-06-20 20:34       ` Rainer M Krug
  2014-06-21 19:44         ` Charles C. Berry
  0 siblings, 1 reply; 16+ messages in thread
From: Rainer M Krug @ 2014-06-20 20:34 UTC (permalink / raw)
  To: Charles Berry; +Cc: emacs-orgmode, Eric Schulte


[-- Attachment #1.1: Type: text/plain, Size: 156 bytes --]

Attached please find =the reworked patch. 

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

Cheers,

Rainer

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: The Patch --]
[-- Type: text/x-patch, Size: 3277 bytes --]

From 2989c5890736099521a077d3b15d2ccc37c88b67 Mon Sep 17 00:00:00 2001
From: "Rainer M. Krug" <R.M.Krug@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


[-- Attachment #1.3: Type: text/plain, Size: 2714 bytes --]




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

> Charles Berry <ccberry@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:      Rainer@krugs.de

Skype:      RMkrug

PGP: 0x0F52F982

[-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-20 20:34       ` Rainer M Krug
@ 2014-06-21 19:44         ` Charles C. Berry
  2014-06-22  3:39           ` Aaron Ecay
                             ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Charles C. Berry @ 2014-06-21 19:44 UTC (permalink / raw)
  To: Rainer M Krug; +Cc: emacs-orgmode, Eric Schulte

On Fri, 20 Jun 2014, Rainer M Krug wrote:

> Attached please find =the reworked patch.
>
> 1) uses local() and closes connection
> 2) does not leave a variable cal;led file behind
>

Seems to work on a few test cases.

I'd move the strings to defconsts.

Putting a newline before each `%S' will outdent the first line of 
:var value when it is echoed in the session log so it will be easier to 
view.

===

With Eric's input on this:

Would you also change this at the end of org-babel-R-assign-elisp:

                         res})" name file header row-names max))))
-    (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
+    (format "%s <- %S" name
+	    (if (stringp value) value (prin1-to-string value)))))

??

I think this is innocuous for plain strings with no internal quotes, but 
allows almost anything to be passed to R as a string.

So
 	:var x=[1 2 3 "4" 5]

will be passed to R as x <- "[1 2 3 \"4\" 5]"

instead of x <- "[1 2 3 "4" 5] which throws an error.

And this just works:

#+NAME: latex-capt
#+BEGIN_SRC latex
   \begin{math}
     y = X\beta,
     \mbox{where } \beta = %beta%
   \end{math}
#+END_SRC

#+BEGIN_SRC R :var ytxt=latex-capt :results raw :wrap latex
beta <- 1.234
sub("%beta%",beta,ytxt)
#+END_SRC


HTH,

Chuck

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-21 19:44         ` Charles C. Berry
@ 2014-06-22  3:39           ` Aaron Ecay
  2014-06-23 10:46             ` Rainer M Krug
  2014-06-23  8:57           ` Rainer M Krug
                             ` (2 subsequent siblings)
  3 siblings, 1 reply; 16+ messages in thread
From: Aaron Ecay @ 2014-06-22  3:39 UTC (permalink / raw)
  To: Charles C. Berry, Rainer M Krug; +Cc: emacs-orgmode, Eric Schulte

Hi Rainer,

2014ko ekainak 21an, "Charles C. Berry"-ek idatzi zuen:
> 
> On Fri, 20 Jun 2014, Rainer M Krug wrote:
> 
>> Attached please find =the reworked patch.
>> 
>> 1) uses local() and closes connection
>> 2) does not leave a variable cal;led file behind
>> 
> 
> Seems to work on a few test cases.
> 
> I'd move the strings to defconsts.
> 
> Putting a newline before each `%S' will outdent the first line of 
> :var value when it is echoed in the session log so it will be easier to 
> view.

+1 to both of the above suggestions but otherwise the patch looks good
and I think it should be applied.

-- 
Aaron Ecay

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-21 19:44         ` Charles C. Berry
  2014-06-22  3:39           ` Aaron Ecay
@ 2014-06-23  8:57           ` Rainer M Krug
  2014-06-23  9:14           ` Rainer M Krug
  2014-06-23 10:45           ` Rainer M Krug
  3 siblings, 0 replies; 16+ messages in thread
From: Rainer M Krug @ 2014-06-23  8:57 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode, Eric Schulte

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

"Charles C. Berry" <ccberry@ucsd.edu> writes:

> On Fri, 20 Jun 2014, Rainer M Krug wrote:
>
>> Attached please find =the reworked patch.
>>
>> 1) uses local() and closes connection
>> 2) does not leave a variable cal;led file behind
>>
>
> Seems to work on a few test cases.
>
> I'd move the strings to defconsts.
>
> Putting a newline before each `%S' will outdent the first line of 
> :var value when it is echoed in the session log so it will be easier to 
> view.

Done

>
> ===
>
> With Eric's input on this:
>
> Would you also change this at the end of org-babel-R-assign-elisp:
>
>                          res})" name file header row-names max))))
> -    (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
> +    (format "%s <- %S" name
> +	    (if (stringp value) value (prin1-to-string value)))))
>

Good idea - done. but as far as I understand the help for the function
format %S

,----
| %S means print any object as an s-expression (using `prin1').
`----

the second line is redy=undant, as the function format is doing it
anyway?

I'll leave the second line out and replace it with =value= instead, but
please correct meif I am missing something here.

Thanks,

Rainer

> ??
>
> I think this is innocuous for plain strings with no internal quotes, but 
> allows almost anything to be passed to R as a string.
>
> So
>  	:var x=[1 2 3 "4" 5]
>
> will be passed to R as x <- "[1 2 3 \"4\" 5]"
>
> instead of x <- "[1 2 3 "4" 5] which throws an error.
>
> And this just works:
>
> #+NAME: latex-capt
> #+BEGIN_SRC latex
>    \begin{math}
>      y = X\beta,
>      \mbox{where } \beta = %beta%
>    \end{math}
> #+END_SRC
>
> #+BEGIN_SRC R :var ytxt=latex-capt :results raw :wrap latex
> beta <- 1.234
> sub("%beta%",beta,ytxt)
> #+END_SRC
>
>
> 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:      Rainer@krugs.de

Skype:      RMkrug

PGP: 0x0F52F982

[-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-21 19:44         ` Charles C. Berry
  2014-06-22  3:39           ` Aaron Ecay
  2014-06-23  8:57           ` Rainer M Krug
@ 2014-06-23  9:14           ` Rainer M Krug
  2014-06-23 17:56             ` Charles C. Berry
  2014-06-23 10:45           ` Rainer M Krug
  3 siblings, 1 reply; 16+ messages in thread
From: Rainer M Krug @ 2014-06-23  9:14 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode, Eric Schulte

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

One more question concerning the variable transfer of tables:

,----
| 	(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 <- 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))))
`----

It seems that the two variable transfer routines only differ slightly:

1. routine: =header= is present (TRUE) and defined in table when passed
as =colnames= while

2. routine: =header= is not present (FALSE) and =colnames= is generated
as V1 .. Vn

This generation is actually not needed as this is the R default value
which is used when =colnames= is not supplied.

Also: I do not undestand why the argument =fill= is TRUE in the second
routine. From R:

,----
|     fill: logical. If ‘TRUE’ then in case the rows have unequal length,
|           blank fields are implicitly added.  See ‘Details’.
`----

If I understands tables in org correctly, this is not needed as the rows
always have equal length?

so if I am not mistaken the following lines could be deleted:

,----
|                            row.names = %s,
|                            sep       = \"\\t\",
|                            as.is     = TRUE,
| -                          fill      = TRUE,
| -                          col.names = paste(\"V\", seq_len(%d), sep =\"\")
`----

Or am I missing something here?

Cheers,

Rainer


"Charles C. Berry" <ccberry@ucsd.edu> writes:

> On Fri, 20 Jun 2014, Rainer M Krug wrote:
>
>> Attached please find =the reworked patch.
>>
>> 1) uses local() and closes connection
>> 2) does not leave a variable cal;led file behind
>>
>
> Seems to work on a few test cases.
>
> I'd move the strings to defconsts.
>
> Putting a newline before each `%S' will outdent the first line of 
> :var value when it is echoed in the session log so it will be easier to 
> view.
>
> ===
>
> With Eric's input on this:
>
> Would you also change this at the end of org-babel-R-assign-elisp:
>
>                          res})" name file header row-names max))))
> -    (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
> +    (format "%s <- %S" name
> +	    (if (stringp value) value (prin1-to-string value)))))
>
> ??
>
> I think this is innocuous for plain strings with no internal quotes, but 
> allows almost anything to be passed to R as a string.
>
> So
>  	:var x=[1 2 3 "4" 5]
>
> will be passed to R as x <- "[1 2 3 \"4\" 5]"
>
> instead of x <- "[1 2 3 "4" 5] which throws an error.
>
> And this just works:
>
> #+NAME: latex-capt
> #+BEGIN_SRC latex
>    \begin{math}
>      y = X\beta,
>      \mbox{where } \beta = %beta%
>    \end{math}
> #+END_SRC
>
> #+BEGIN_SRC R :var ytxt=latex-capt :results raw :wrap latex
> beta <- 1.234
> sub("%beta%",beta,ytxt)
> #+END_SRC
>
>
> 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:      Rainer@krugs.de

Skype:      RMkrug

PGP: 0x0F52F982

[-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-21 19:44         ` Charles C. Berry
                             ` (2 preceding siblings ...)
  2014-06-23  9:14           ` Rainer M Krug
@ 2014-06-23 10:45           ` Rainer M Krug
  2014-06-23 18:25             ` Charles C. Berry
  3 siblings, 1 reply; 16+ messages in thread
From: Rainer M Krug @ 2014-06-23 10:45 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode, Eric Schulte


[-- Attachment #1.1: Type: text/plain, Size: 2497 bytes --]

"Charles C. Berry" <ccberry@ucsd.edu> writes:

V> On Fri, 20 Jun 2014, Rainer M Krug wrote:
>
>> Attached please find =the reworked patch.
>>
>> 1) uses local() and closes connection
>> 2) does not leave a variable cal;led file behind
>>
>
> Seems to work on a few test cases.
>
> I'd move the strings to defconsts.
>
> Putting a newline before each `%S' will outdent the first line of 
> :var value when it is echoed in the session log so it will be easier to 
> view.
>
> ===
>
> With Eric's input on this:
>
> Would you also change this at the end of org-babel-R-assign-elisp:
>
>                          res})" name file header row-names max))))
> -    (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
> +    (format "%s <- %S" name
> +	    (if (stringp value) value (prin1-to-string value)))))
>
> ??
>
> I think this is innocuous for plain strings with no internal quotes, but 
> allows almost anything to be passed to R as a string.
>
> So
>  	:var x=[1 2 3 "4" 5]
>
> will be passed to R as x <- "[1 2 3 \"4\" 5]"
>
> instead of x <- "[1 2 3 "4" 5] which throws an error.
>
> And this just works:
>
> #+NAME: latex-capt
> #+BEGIN_SRC latex
>    \begin{math}
>      y = X\beta,
>      \mbox{where } \beta = %beta%
>    \end{math}
> #+END_SRC
>
> #+BEGIN_SRC R :var ytxt=latex-capt :results raw :wrap latex
> beta <- 1.234
> sub("%beta%",beta,ytxt)
> #+END_SRC

OK - attached please find two patches:

0001-lisp-ob-R.el-Fix-tangling-with-tables.patch
0002-Make-transfer-of-values-from-R-type-aware.patch

The first one fixes tangling with tables including discussed
suggestions, the second one moves the definition of the R code into
defconst and introduces type awareness for the transfer of *values*, but
not tables. I reworked your suggestion and now integer are transferred
to R as integers (L) and float as double.
This already worked for tables.

Could you please check the second patch? In my checks everything worked
as expected.

Cheers,

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:      Rainer@krugs.de

Skype:      RMkrug

PGP: 0x0F52F982

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: 0001-lisp-ob-R.el-Fix-tangling-with-tables.patch --]
[-- Type: text/x-patch, Size: 3564 bytes --]

From 38c029b38c85d9f9d35d0867b332eebc3daf1aca Mon Sep 17 00:00:00 2001
From: "Rainer M. Krug" <R.M.Krug@gmail.com>
Date: Fri, 20 Jun 2014 22:19:59 +0200
Subject: [PATCH 1/2] 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 | 52 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 20 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index a3ae1ec..c77a103 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -190,32 +190,44 @@ 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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.3: 0002-Make-transfer-of-values-from-R-type-aware.patch --]
[-- Type: text/x-patch, Size: 4341 bytes --]

From e62a75e7227027c406d23fa9f8ac594db2ce81d6 Mon Sep 17 00:00:00 2001
From: "Rainer M. Krug" <R.M.Krug@gmail.com>
Date: Mon, 23 Jun 2014 12:11:59 +0200
Subject: [PATCH 2/2] Make transfer of values from R type aware

* lisp/ob-R.el (org-babel-R-assign-elisp): Added different cases for
transfer of integer, float, string and other variables to R so that
now integer values are transferred as integers (L) and stored in R as
such.  This change is backward compatible as integer values are
numerical values in R.
Moved definition of R functions for transfer of tables into defconst
ob-R-transfer-variable-table-with-header and
ob-R-transfer-variable-table-without-header.
---
 lisp/ob-R.el | 78 ++++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 47 insertions(+), 31 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index c77a103..92c1dbc 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -93,6 +93,44 @@ this variable.")
     (when (and session (string-match "^\\*\\(.+?\\)\\*$" session))
       (save-match-data (org-babel-R-initiate-session session nil)))))
 
+(defconst ob-R-transfer-variable-table-with-header
+  "%s <- local({
+     con <- textConnection(
+       %S
+     )
+     res <- read.table(
+       con,
+       header    = %s,
+       row.names = %s,
+       sep       = \"\\t\",
+       as.is     = TRUE
+     )
+     close(con)
+     res
+   })"
+  "R code used to transfer a table defined as a variable from org to R. 
+This function is used when the table contains a header.")
+
+(defconst ob-R-transfer-variable-table-without-header
+  "%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
+   })"
+  "R code used to transfer a table defined as a variable from org to R. 
+This function is used when the table does not contain a header.")
+
 (defun org-babel-expand-body:R (body params &optional graphics-file)
   "Expand BODY according to PARAMS, return the expanded body."
   (mapconcat 'identity
@@ -198,37 +236,15 @@ This function is called by `org-babel-execute-src-block'."
 			  "TRUE" "FALSE"))
 	      (row-names (if rownames-p "1" "NULL")))
 	  (if (= max min)
-	      (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))))
+	      (format ob-R-transfer-variable-table-with-header
+		      name file header row-names)
+	    (format ob-R-transfer-variable-table-without-header
+		    name file header row-names max))))
+    (if (integerp value) (format "%s <- %s" name (concat (number-to-string value) "L"))
+      (if (floatp value) (format "%s <- %s" name value)
+	(if (stringp value) (format "%s <- %S" name value) 
+	  (format "%s <- %S" name (prin1-to-string value)))))))
+
 
 (defvar ess-ask-for-ess-directory) ; dynamically scoped
 (defun org-babel-R-initiate-session (session params)
-- 
2.0.0


[-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-22  3:39           ` Aaron Ecay
@ 2014-06-23 10:46             ` Rainer M Krug
  0 siblings, 0 replies; 16+ messages in thread
From: Rainer M Krug @ 2014-06-23 10:46 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode, Eric Schulte

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

Aaron Ecay <aaronecay@gmail.com> writes:

> Hi Rainer,
>
> 2014ko ekainak 21an, "Charles C. Berry"-ek idatzi zuen:
>> 
>> On Fri, 20 Jun 2014, Rainer M Krug wrote:
>> 
>>> Attached please find =the reworked patch.
>>> 
>>> 1) uses local() and closes connection
>>> 2) does not leave a variable cal;led file behind
>>> 
>> 
>> Seems to work on a few test cases.
>> 
>> I'd move the strings to defconsts.
>> 
>> Putting a newline before each `%S' will outdent the first line of 
>> :var value when it is echoed in the session log so it will be easier to 
>> view.
>
> +1 to both of the above suggestions but otherwise the patch looks good
> and I think it should be applied.

Thanks - a second patch has been send with the additional suggestions.

Rainer

-- 
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:      Rainer@krugs.de

Skype:      RMkrug

PGP: 0x0F52F982

[-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-23  9:14           ` Rainer M Krug
@ 2014-06-23 17:56             ` Charles C. Berry
  2014-06-24 12:08               ` Rainer M Krug
  0 siblings, 1 reply; 16+ messages in thread
From: Charles C. Berry @ 2014-06-23 17:56 UTC (permalink / raw)
  To: Rainer M Krug; +Cc: emacs-orgmode, Eric Schulte

[-- Attachment #1: Type: TEXT/PLAIN, Size: 3740 bytes --]

On Mon, 23 Jun 2014, Rainer M Krug wrote:

> One more question concerning the variable transfer of tables:
>
> ,----
> | 	(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 <- 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))))
> `----
>
> It seems that the two variable transfer routines only differ slightly:
>
> 1. routine: =header= is present (TRUE) and defined in table when passed
> as =colnames= while
>
> 2. routine: =header= is not present (FALSE) and =colnames= is generated
> as V1 .. Vn
>
> This generation is actually not needed as this is the R default value
> which is used when =colnames= is not supplied.
>
> Also: I do not undestand why the argument =fill= is TRUE in the second
> routine. From R:
>
> ,----
> |     fill: logical. If ‘TRUE’ then in case the rows have unequal length,
> |           blank fields are implicitly added.  See ‘Details’.
> `----
>
> If I understands tables in org correctly, this is not needed as the rows
> always have equal length?
>
> so if I am not mistaken the following lines could be deleted:
>
> ,----
> |                            row.names = %s,
> |                            sep       = \"\\t\",
> |                            as.is     = TRUE,
> | -                          fill      = TRUE,
> | -                          col.names = paste(\"V\", seq_len(%d), sep =\"\")
> `----
>
> Or am I missing something here?

You need not pass a table - this

#+BEGIN_SRC R	:var x='(1 2 3)(4 5))

will produce a data.frame with two rows with is.na(x[2,2]) being TRUE.


Re col.names, from help("read.table")

      The number of data columns is determined by looking at the first
      five lines of input (or the whole file if it has less than five
      lines), or from the length of 'col.names' if it is specified and
      is longer.  This could conceivably be wrong if 'fill' or
      'blank.lines.skip' are true, so specify 'col.names' if necessary
      (as in the 'Examples').


This block

#+BEGIN_SRC R :var x=(append (make-vector 10 '(1 2)) '((1 2 3)))
x
#+END_SRC

makes an 11 by 3 data.frame with all(is.na(x[,1:10]))

but taking away the col.names arg will cause it to make a 12 by 2 
data.frame with x[12,1]==3 and is.na(x[12,2]).

so leave the col.names as is, I think.

Without fill, read.table will throw an error in that block

Chuck

[rest deleted]


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-23 10:45           ` Rainer M Krug
@ 2014-06-23 18:25             ` Charles C. Berry
  2014-06-24 12:28               ` Rainer M Krug
  0 siblings, 1 reply; 16+ messages in thread
From: Charles C. Berry @ 2014-06-23 18:25 UTC (permalink / raw)
  To: Rainer M Krug; +Cc: emacs-orgmode, Eric Schulte

On Mon, 23 Jun 2014, Rainer M Krug wrote:

> "Charles C. Berry" <ccberry@ucsd.edu> writes:
>
> V> On Fri, 20 Jun 2014, Rainer M Krug wrote:
>>
>>> Attached please find =the reworked patch.
>>>
>>> 1) uses local() and closes connection
>>> 2) does not leave a variable cal;led file behind
>>>
>>

[deleted]

>
> OK - attached please find two patches:
>
> 0001-lisp-ob-R.el-Fix-tangling-with-tables.patch
> 0002-Make-transfer-of-values-from-R-type-aware.patch
>
> The first one fixes tangling with tables including discussed
> suggestions, the second one moves the definition of the R code into
> defconst and introduces type awareness for the transfer of *values*, but
> not tables. I reworked your suggestion and now integer are transferred
> to R as integers (L) and float as double.
> This already worked for tables.
>
> Could you please check the second patch? In my checks everything worked
> as expected.

I think these are good to go.

I checked several cases and all seemed to work as expected.

Some comments on this and your recent posts in this thread:

I see you took the suggestion to use (prin1-to-string value) on the 
'left-over' objects. With that stuff like :var x=[1 2 3] will work. :-)

The only failure I could trigger was with this block:

#+BEGIN_SRC R :session :var x=(org-export-get-backend 'latex)
x
#+END_SRC

which triggered 'Eval buffer', added a bunch of ^G's to my session log, 
and hung until I ran C-g (keyboard-quit).

When run with :session none, it works. I suspected the long string tripped 
over a limitation or bug in iESS-mode or comint-mode, but this runs:

#+BEGIN_SRC R :session :var x=(append (make-vector 10000 '(1 2)) '((1 2 3))) 
:results output
summary(x)
#+END_SRC

so I am not sure what gives.

Chuck

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-23 17:56             ` Charles C. Berry
@ 2014-06-24 12:08               ` Rainer M Krug
  0 siblings, 0 replies; 16+ messages in thread
From: Rainer M Krug @ 2014-06-24 12:08 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode, Eric Schulte

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

"Charles C. Berry" <ccberry@ucsd.edu> writes:

> On Mon, 23 Jun 2014, Rainer M Krug wrote:
>
>> One more question concerning the variable transfer of tables:
>>
>> ,----
>> | 	(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 <- 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))))
>> `----
>>
>> It seems that the two variable transfer routines only differ slightly:
>>
>> 1. routine: =header= is present (TRUE) and defined in table when passed
>> as =colnames= while
>>
>> 2. routine: =header= is not present (FALSE) and =colnames= is generated
>> as V1 .. Vn
>>
>> This generation is actually not needed as this is the R default value
>> which is used when =colnames= is not supplied.
>>
>> Also: I do not undestand why the argument =fill= is TRUE in the second
>> routine. From R:
>>
>> ,----
>> |     fill: logical. If ‘TRUE’ then in case the rows have unequal length,
>> |           blank fields are implicitly added.  See ‘Details’.
>> `----
>>
>> If I understands tables in org correctly, this is not needed as the rows
>> always have equal length?
>>
>> so if I am not mistaken the following lines could be deleted:
>>
>> ,----
>> |                            row.names = %s,
>> |                            sep       = \"\\t\",
>> |                            as.is     = TRUE,
>> | -                          fill      = TRUE,
>> | -                          col.names = paste(\"V\", seq_len(%d), sep =\"\")
>> `----
>>
>> Or am I missing something here?
>
> You need not pass a table - this
>
> #+BEGIN_SRC R	:var x='(1 2 3)(4 5))

I always forget that there are other objects wich can be passed from org
to R.

>
> will produce a data.frame with two rows with is.na(x[2,2]) being TRUE.
>
>
> Re col.names, from help("read.table")
>
>       The number of data columns is determined by looking at the first
>       five lines of input (or the whole file if it has less than five
>       lines), or from the length of 'col.names' if it is specified and
>       is longer.  This could conceivably be wrong if 'fill' or
>       'blank.lines.skip' are true, so specify 'col.names' if necessary
>       (as in the 'Examples').
>
>
> This block
>
> #+BEGIN_SRC R :var x=(append (make-vector 10 '(1 2)) '((1 2 3)))
> x
> #+END_SRC
>
> makes an 11 by 3 data.frame with all(is.na(x[,1:10]))
>
> but taking away the col.names arg will cause it to make a 12 by 2 
> data.frame with x[12,1]==3 and is.na(x[12,2]).
>
> so leave the col.names as is, I think.

I will - promised.
 
>
> Without fill, read.table will throw an error in that block

This is true for these cases - I was only thinking about tables which
can be passed to R

Thanks,

Rainer

>
> Chuck
>
> [rest deleted]
>

-- 
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:      Rainer@krugs.de

Skype:      RMkrug

PGP: 0x0F52F982

[-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] -for review- Tangling with variable transfer of variables
  2014-06-23 18:25             ` Charles C. Berry
@ 2014-06-24 12:28               ` Rainer M Krug
  0 siblings, 0 replies; 16+ messages in thread
From: Rainer M Krug @ 2014-06-24 12:28 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode, Eric Schulte

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

"Charles C. Berry" <ccberry@ucsd.edu> writes:

> On Mon, 23 Jun 2014, Rainer M Krug wrote:
>
>> "Charles C. Berry" <ccberry@ucsd.edu> writes:
>>
>> V> On Fri, 20 Jun 2014, Rainer M Krug wrote:
>>>
>>>> Attached please find =the reworked patch.
>>>>
>>>> 1) uses local() and closes connection
>>>> 2) does not leave a variable cal;led file behind
>>>>
>>>
>
> [deleted]
>
>>
>> OK - attached please find two patches:
>>
>> 0001-lisp-ob-R.el-Fix-tangling-with-tables.patch
>> 0002-Make-transfer-of-values-from-R-type-aware.patch
>>
>> The first one fixes tangling with tables including discussed
>> suggestions, the second one moves the definition of the R code into
>> defconst and introduces type awareness for the transfer of *values*, but
>> not tables. I reworked your suggestion and now integer are transferred
>> to R as integers (L) and float as double.
>> This already worked for tables.
>>
>> Could you please check the second patch? In my checks everything worked
>> as expected.
>
> I think these are good to go.

Great - Eric - could you apply them if you are happy as well?

>
> I checked several cases and all seemed to work as expected.

Good to know.

>
> Some comments on this and your recent posts in this thread:
>
> I see you took the suggestion to use (prin1-to-string value) on the 
> 'left-over' objects. With that stuff like :var x=[1 2 3] will work. :-)
>
> The only failure I could trigger was with this block:
>
> #+BEGIN_SRC R :session :var x=(org-export-get-backend 'latex)
> x
> #+END_SRC
>
> which triggered 'Eval buffer', added a bunch of ^G's to my session log, 
> and hung until I ran C-g (keyboard-quit).
>
> When run with :session none, it works. I suspected the long string tripped 
> over a limitation or bug in iESS-mode or comint-mode, but this runs:

Am I wrong, or is this a rather unusual usage scenario? 

When I copy the command executed into an R session outside emacs/ESS, it
gives me a new line with a +, so the command is not complete and
requires a " to complete. But then the vartiable only contains a part of
the string. So I guess there is a problem with escaping the string.

When I paste it into an R session in emacs, I get these ^G.

Given that your example below (with a more usual variable?) works, and
the error above is also present when using the actual implementation of
the variable transfer, I would leave it as an unsolved mystery and
investigate afterwards.

Cheers,

Rainer

>
> #+BEGIN_SRC R :session :var x=(append (make-vector 10000 '(1 2)) '((1 2 3))) 
> :results output
> summary(x)
> #+END_SRC
>
> so I am not sure what gives.
>
> 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:      Rainer@krugs.de

Skype:      RMkrug

PGP: 0x0F52F982

[-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2014-06-24 12:28 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-19 20:19 [PATCH] -for review- Tangling with variable transfer of variables Rainer M Krug
2014-06-19 21:09 ` Aaron Ecay
2014-06-19 23:23   ` Charles Berry
2014-06-20 13:15     ` Rainer M Krug
2014-06-20 20:34       ` Rainer M Krug
2014-06-21 19:44         ` Charles C. Berry
2014-06-22  3:39           ` Aaron Ecay
2014-06-23 10:46             ` Rainer M Krug
2014-06-23  8:57           ` Rainer M Krug
2014-06-23  9:14           ` Rainer M Krug
2014-06-23 17:56             ` Charles C. Berry
2014-06-24 12:08               ` Rainer M Krug
2014-06-23 10:45           ` Rainer M Krug
2014-06-23 18:25             ` Charles C. Berry
2014-06-24 12:28               ` Rainer M Krug
2014-06-20 13:07   ` Rainer M Krug

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).