Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-24 Thread Rainer M Krug
Charles C. Berry ccbe...@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:  rai...@krugs.de

Skype:  RMkrug

PGP: 0x0F52F982


pgpLPf4Hgjoer.pgp
Description: PGP signature


Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-24 Thread Rainer M Krug
Charles C. Berry ccbe...@ucsd.edu writes:

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

 Charles C. Berry ccbe...@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 1 '(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:  rai...@krugs.de

Skype:  RMkrug

PGP: 0x0F52F982


pgp2M0TITz2zx.pgp
Description: PGP signature


Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-23 Thread Rainer M Krug
Charles C. Berry ccbe...@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:  rai...@krugs.de

Skype:  RMkrug

PGP: 0x0F52F982


pgp7cKpKLik5z.pgp
Description: PGP signature


Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-23 Thread Rainer M Krug
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 ccbe...@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:  rai...@krugs.de

Skype:  RMkrug

PGP: 0x0F52F982


pgpz3VqNs_wPq.pgp
Description: PGP signature


Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-23 Thread Rainer M Krug
Charles C. Berry ccbe...@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:  rai...@krugs.de

Skype:  RMkrug

PGP: 0x0F52F982
From 38c029b38c85d9f9d35d0867b332eebc3daf1aca 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 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({
+

Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-23 Thread Rainer M Krug
Aaron Ecay aarone...@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:  rai...@krugs.de

Skype:  RMkrug

PGP: 0x0F52F982


pgpR9pZQV5Uyo.pgp
Description: PGP signature


Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-23 Thread Charles C. Berry

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]



Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-23 Thread Charles C. Berry

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


Charles C. Berry ccbe...@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 1 '(1 2)) '((1 2 3))) 
:results output

summary(x)
#+END_SRC

so I am not sure what gives.

Chuck




Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-21 Thread Charles C. Berry

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



Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-21 Thread Aaron Ecay
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



Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-20 Thread Rainer M Krug
Aaron Ecay aarone...@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.k...@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:  rai...@krugs.de

Skype:  RMkrug

PGP: 0x0F52F982


pgpvJfQibcQ3e.pgp
Description: PGP signature


Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-20 Thread Rainer M Krug
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


pgpf9dVoQtrAu.pgp
Description: PGP signature


Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-20 Thread Rainer M Krug
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 

[O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-19 Thread Rainer M Krug
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.k...@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


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

Skype:  RMkrug

PGP: 0x0F52F982


pgphxWvBI0qck.pgp
Description: PGP signature


Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-19 Thread Aaron Ecay
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.k...@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



Re: [O] [PATCH] -for review- Tangling with variable transfer of variables

2014-06-19 Thread Charles Berry
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