From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rainer M Krug Subject: Re: [patch] [babel] Patches to fix tangling and variable transfer of tables in R Date: Tue, 01 Jul 2014 12:51:25 +0200 Message-ID: References: <87ionimv4p.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha1; protocol="application/pgp-signature" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:49831) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X1vf0-0003gg-Rg for emacs-orgmode@gnu.org; Tue, 01 Jul 2014 06:51:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X1vev-0006dA-No for emacs-orgmode@gnu.org; Tue, 01 Jul 2014 06:51:38 -0400 Received: from mail-wg0-f50.google.com ([74.125.82.50]:33969) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X1vev-0006d0-EE for emacs-orgmode@gnu.org; Tue, 01 Jul 2014 06:51:33 -0400 Received: by mail-wg0-f50.google.com with SMTP id m15so9248992wgh.33 for ; Tue, 01 Jul 2014 03:51:32 -0700 (PDT) Received: from Rainers-MacBook-Pro-3.local (arn78-1-88-186-171-7.fbx.proxad.net. [88.186.171.7]) by mx.google.com with ESMTPSA id s14sm13512606wij.1.2014.07.01.03.51.30 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Jul 2014 03:51:31 -0700 (PDT) In-Reply-To: <87ionimv4p.fsf@nicolasgoaziou.fr> (Nicolas Goaziou's message of "Mon, 30 Jun 2014 14:36:54 +0200") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Nicolas Goaziou writes: > Hello, > > Rainer M Krug writes: > >> Attached please find two patches ready for application to fix that table >> variables were not usable adter tangling as temporary files were used. >> These patches were discussed in the thread "[babel][PATCHES] ob-R >> patches for review" > > I have not much to say about the feature, since I don't use R. Usual > nitpicking follows. > >> + (if (integerp value) (format "%s <- %s" name (concat (number-to-str= ing value) "L")) >> + (if (floatp value) (format "%s <- %s" name value) >> + (if (stringp value) (format "%s <- %S" name value)=20 >> + (format "%s <- %S" name (prin1-to-string value))))))) > > I think a `cond' would be more readable here. Amended patches attached. > > > Regards, =2D-=20 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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-lisp-ob-R.el-Fix-tangling-with-tables.patch Content-Transfer-Encoding: quoted-printable From=2038c029b38c85d9f9d35d0867b332eebc3daf1aca Mon Sep 17 00:00:00 2001 From: "Rainer M. Krug" 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. =2D-- 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 =2D-- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -190,32 +190,44 @@ This function is called by `org-babel-execute-src-blo= ck'." (if (listp value) (let* ((lengths (mapcar 'length (org-remove-if-not 'sequencep value)= )) (max (if lengths (apply 'max lengths) 0)) =2D (min (if lengths (apply 'min lengths) 0)) =2D (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))) =2D (with-temp-file transition-file =2D (insert =2D (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)) =2D "\n")) =2D (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 (=3D max min) =2D (format "%s <- read.table(\"%s\", =2D header=3D%s, =2D row.names=3D%s, =2D sep=3D\"\\t\", =2D as.is=3DTRUE)" name file header row-names) =2D (format "%s <- read.table(\"%s\", =2D header=3D%s, =2D row.names=3D%s, =2D sep=3D\"\\t\", =2D as.is=3DTRUE, =2D fill=3DTRUE, =2D col.names =3D paste(\"V\", seq_len(%d), sep =3D\"\"))" =2D name file header row-names max)))) + (format "%s <- local({ + con <- textConnection( + %S + ) + res <- read.table( + con, + header =3D %s, + row.names =3D %s, + sep =3D \"\\t\", + as.is =3D TRUE + ) + close(con) + res + })" name file header row-names) + (format "%s <- local({ + con <- textConnection( + %S + ) + res <- read.table( + con, + header =3D %s, + row.names =3D %s, + sep =3D \"\\t\", + as.is =3D TRUE, + fill =3D TRUE, + col.names =3D paste(\"V\", seq_len(%d), sep =3D\"= \") + ) + close(con) + res + })" name file header row-names max)))) (format "%s <- %s" name (org-babel-R-quote-tsv-field value)))) =20 (defvar ess-ask-for-ess-directory) ; dynamically scoped =2D-=20 2.0.0 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0002-Make-transfer-of-values-from-R-type-aware.patch Content-Transfer-Encoding: quoted-printable From=20551d003d15c5cc9d3792909a12247fca58ae8d84 Mon Sep 17 00:00:00 2001 From: "Rainer M. Krug" 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. =2D-- 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..c9f04ea 100644 =2D-- 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))))) =20 +(defconst ob-R-transfer-variable-table-with-header + "%s <- local({ + con <- textConnection( + %S + ) + res <- read.table( + con, + header =3D %s, + row.names =3D %s, + sep =3D \"\\t\", + as.is =3D TRUE + ) + close(con) + res + })" + "R code used to transfer a table defined as a variable from org to R.=20 +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 =3D %s, + row.names =3D %s, + sep =3D \"\\t\", + as.is =3D TRUE, + fill =3D TRUE, + col.names =3D paste(\"V\", seq_len(%d), sep =3D\"\") + ) + close(con) + res + })" + "R code used to transfer a table defined as a variable from org to R.=20 +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-blo= ck'." "TRUE" "FALSE")) (row-names (if rownames-p "1" "NULL"))) (if (=3D max min) =2D (format "%s <- local({ =2D con <- textConnection( =2D %S =2D ) =2D res <- read.table( =2D con, =2D header =3D %s, =2D row.names =3D %s, =2D sep =3D \"\\t\", =2D as.is =3D TRUE =2D ) =2D close(con) =2D res =2D })" name file header row-names) =2D (format "%s <- local({ =2D con <- textConnection( =2D %S =2D ) =2D res <- read.table( =2D con, =2D header =3D %s, =2D row.names =3D %s, =2D sep =3D \"\\t\", =2D as.is =3D TRUE, =2D fill =3D TRUE, =2D col.names =3D paste(\"V\", seq_len(%d), sep =3D= \"\") =2D ) =2D close(con) =2D res =2D })" name file header row-names max)))) =2D (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)))) + (cond ((integerp value) (format "%s <- %s" name (concat (number-to-str= ing value) "L"))) + ((floatp value) (format "%s <- %s" name value)) + ((stringp value) (format "%s <- %S" name value)) + (t (format "%s <- %S" name (prin1-to-string value)))))) + =20 (defvar ess-ask-for-ess-directory) ; dynamically scoped (defun org-babel-R-initiate-session (session params) =2D-=20 2.0.0 --=-=-=-- --==-=-= Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.22 (Darwin) iQEcBAEBAgAGBQJTspKxAAoJENvXNx4PUvmC2EkIAKtve6RyYyihMYt4I5/ORoFN uWC3KHF1PK/eQzvmFqS+k9upRWnNGvC/hr5WGkAO64C3ZZg8XjCBpNH0Sb0Z/OvG XB1cYATrg03Zh9JAWiY0M/eJEwNEIxfDlJAVlhxOWqozRShIAJ8gSzNwvcaG46gu WZ/W8kasdbhHsz7YE31Z8P48t77+CT99wbvJacB1Tj2Y6G+I6jZK+cmcmQnf+PJV vgwuA6fNdOAdNR6sgrhXd9erMQFId0p8RtHxfuR8fZuczSNhXnllBe7vahnvns/E ddYarqT9cTEcqt6m0svUFb/qfcwZ2ITLw95xO/UBgCK7EZM9Mh/DQzDrlZw5WGo= =FRug -----END PGP SIGNATURE----- --==-=-=--