emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Rainer M Krug <Rainer@krugs.de>
To: "Charles C. Berry" <ccberry@ucsd.edu>
Cc: emacs-orgmode@gnu.org, Eric Schulte <schulte.eric@gmail.com>
Subject: Re: [PATCH] -for review- Tangling with variable transfer of variables
Date: Mon, 23 Jun 2014 12:45:32 +0200	[thread overview]
Message-ID: <m2egyfoqer.fsf@krugs.de> (raw)
In-Reply-To: <alpine.OSX.2.00.1406211148460.344@charles-berrys-macbook.local> (Charles C. Berry's message of "Sat, 21 Jun 2014 12:44:01 -0700")


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

  parent reply	other threads:[~2014-06-23 10:45 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2014-06-23 18:25             ` Charles C. Berry
2014-06-24 12:28               ` Rainer M Krug
2014-06-20 13:07   ` Rainer M Krug

Reply instructions:

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

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

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

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

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

  git send-email \
    --in-reply-to=m2egyfoqer.fsf@krugs.de \
    --to=rainer@krugs.de \
    --cc=ccberry@ucsd.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=schulte.eric@gmail.com \
    /path/to/YOUR_REPLY

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

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

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

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