From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rainer M Krug Subject: Re: [PATCH] -for review- Tangling with variable transfer of variables Date: Fri, 20 Jun 2014 22:34:13 +0200 Message-ID: References: <87zjh8bo8s.fsf@gmail.com> 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]:47578) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wy5W2-0004a0-GX for emacs-orgmode@gnu.org; Fri, 20 Jun 2014 16:34:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wy5Vx-0007AT-98 for emacs-orgmode@gnu.org; Fri, 20 Jun 2014 16:34:30 -0400 Received: from mail-wi0-f178.google.com ([209.85.212.178]:56563) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wy5Vx-0007AM-0K for emacs-orgmode@gnu.org; Fri, 20 Jun 2014 16:34:25 -0400 Received: by mail-wi0-f178.google.com with SMTP id n15so1381724wiw.5 for ; Fri, 20 Jun 2014 13:34:23 -0700 (PDT) In-Reply-To: (Rainer M. Krug's message of "Fri, 20 Jun 2014 15:15:31 +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: Charles Berry Cc: emacs-orgmode@gnu.org, Eric Schulte --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Attached please find =3Dthe reworked patch.=20 1) uses local() and closes connection 2) does not leave a variable cal;led file behind Cheers, Rainer --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-lisp-ob-R.el-Fix-tangling-with-tables.patch Content-Transfer-Encoding: quoted-printable Content-Description: The Patch From=202989c5890736099521a077d3b15d2ccc37c88b67 Mon Sep 17 00:00:00 2001 From: "Rainer M. Krug" 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. =2D-- 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 =2D-- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -190,32 +190,34 @@ 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/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Rainer M Krug writes: > Charles Berry writes: > >> Aaron Ecay gmail.com> writes: >> >>>=20 >>> Hi Rainer, >>>=20 >>> I have not tested the patch (I rarely use :var), but here are some >>> comments from reading the patch. >>>=20 >> [snip] >>>=20 >>> > (header (if (or (eq (nth 1 value) 'hline) colnames-p) >>> > "TRUE" "FALSE")) >>> > (row-names (if rownames-p "1" "NULL"))) >>> > (if (=3D max min) >>> > - (format "%s <- read.table(\"%s\", >>> > + (format "%s <- read.table(textConnection('%s'), >>>=20 >>> This will fail if =E2=80=98file=E2=80=99 contains an apostrophe charact= er. I guess you >>> need to escape apostrophes in =E2=80=98file=E2=80=99. >>>=20 >> >> 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: >> >> =3D=3D=3D=3D >> (header (if (or (eq (nth 1 value) 'hline) colnames-p) >> "TRUE" "FALSE")) >> (row-names (if rownames-p "1" "NULL"))) >> (if (=3D max min) >> (format " >> %s <-=20 >> local({con.object <-=20 >> %S >> con <- textConnection(con.object) >> res <- read.table(con, >> header=3D%s, >> row.names=3D%s, >> sep=3D\"\\t\", >> as.is=3DTRUE) >> close(con) >> res})"=20 >> name file header row-names) >> >> =3D=3D=3D=3D >> >> 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 >> >> >> >> =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: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.22 (Darwin) iQEcBAEBAgAGBQJTpJrLAAoJENvXNx4PUvmC1VgIAJquHtvzX9nFXPijASLp86tX rt1pUW8iAFD0fjoAX0uLbVVEW8z/N+JaO8GcSSZ7nbKNrVT4HcWP4+MZI7okECQr KypTUygGc+Z2j3SDdlVYahRT6GffSSm/vOY/5++GIgPiyjvcSstsIFNRWenLczqA qduMvDriTpxBLB5EE30KZmhCT2pFooKN79ETzIIbdpv5LZCC3PQUGeIe2ARZSh9o 7+bJRvYBito5dbJZpIVFZGdQVcpGbJLL1Zw6Mh0siGo2n9V0lRAuKSpS6+i004Yr vW12GaGWnUiegh3n86yCr6vpzw/kRvj7y29YC7Y6YwHzRf2Yzp/D8D79BP+omhM= =aXaR -----END PGP SIGNATURE----- --==-=-=--