From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rainer M Krug Subject: [PATCH] Write org variables into own R environment instead of .GlobalEnv Date: Tue, 18 Mar 2014 15:05:06 +0100 Message-ID: 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]:39966) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WPudq-00082K-1Q for emacs-orgmode@gnu.org; Tue, 18 Mar 2014 10:05:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WPudi-0002mM-NH for emacs-orgmode@gnu.org; Tue, 18 Mar 2014 10:05:17 -0400 Received: from mail-we0-f175.google.com ([74.125.82.175]:57713) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WPudi-0002mC-Dv for emacs-orgmode@gnu.org; Tue, 18 Mar 2014 10:05:10 -0400 Received: by mail-we0-f175.google.com with SMTP id q58so5828498wes.20 for ; Tue, 18 Mar 2014 07:05:09 -0700 (PDT) Received: from Rainers-MacBook-Pro-2.local (arn78-1-88-186-171-7.fbx.proxad.net. [88.186.171.7]) by mx.google.com with ESMTPSA id 19sm47538195wjy.17.2014.03.18.07.05.06 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Mar 2014 07:05:07 -0700 (PDT) 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: text/plain Content-Transfer-Encoding: quoted-printable * (org-babel-R-assign-elisp): replaced simple assignments of variables to variables in R with assignments into an environment called 'org' and lock these so that they can not be modified anymore. * (org-babel-expand-body:R): wrap the call of `org-babel-variable-assignments:R' into - detachment of org environment - creation of new environment org to delete the old environment - locking of the org environment to prevent adding of bindings - appending of environment to search path of R These patch implements the writing of org variables into a separate R environment and attaches it to the search path. This solves several not so nice issues: 1) The org variables are now grouped and can be seen via 'ls(org)' in R 2) As the environment org and all bindings are locked, the variables can not be accidentally deleted. They can be overwritten, but they can be =3Drestored by simply deleting the variable in R or by using `org$VARIABLE' instead of `VARIABLE' 3) All variables can be saved by simply calling `save(org, FILENAME' in R which makes it possible to store all variable definitions for tangling in one file. =2D-- lisp/ob-R.el | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index 62aa7f2..da2aea2 100644 =2D-- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -103,8 +103,12 @@ this variable.") (append (when (cdr (assoc :prologue params)) (list (cdr (assoc :prologue params)))) =2D (org-babel-variable-assignments:R params) =2D (list body) + '("try(detach(org), silent=3DTRUE)") + '("org <- new.env()") + (org-babel-variable-assignments:R params) + '("lockEnvironment(org)") + '("attach(org)") + (list body) (when (cdr (assoc :epilogue params)) (list (cdr (assoc :epilogue params))))))) (if graphics-file @@ -203,20 +207,31 @@ 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 <- 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)))) =2D (format "%s <- %s" name (org-babel-R-quote-tsv-field value)))) + (format "assign( + '%s',=20 + read.table(\"%s\", + header=3D%s, + row.names=3D%s, + sep=3D\"\\t\", + as.is=3DTRUE + ), + envir =3D org + ) + lockBinding('%s', org)" name file header row-names = name) + (format "assign( + '%s',=20 + read.table(\"%s\", + header=3D%s, + row.names=3D%s, + sep=3D\"\\t\", + as.is=3DTRUE, + fill=3DTRUE, + col.names =3D paste(\"V\", seq_len(%d), sep =3D\"= \") + ), + envir =3D org + ) + lockBinding('%s', org)" name file header row-names ma= x name)))) + (format "assign('%s', %s, envir =3D org); lockBinding('%s', org)" name= (org-babel-R-quote-tsv-field value) name))) =20 (defvar ess-ask-for-ess-directory) ; dynamically scoped (defun org-babel-R-initiate-session (session params) =2D-=20 1.8.5.2 (Apple Git-48) =2D-=20 Rainer M. Krug email: RMKruggmailcom PGP: 0x0F52F982 --=-=-= Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.22 (Darwin) iQEcBAEBAgAGBQJTKFKXAAoJENvXNx4PUvmCbEQH/R/uKRirnk96vURc6yDIt71q hEFL60Oj2YS09yVQBf9/FUD59PzHQlxUTcmu/Z4FLq+Z9jrzPx0TD+ic3139VDnZ OvnQPPLjfTOPk2q8WMiW5/1XPfNGhY45s6pcGuMDx3ldQI2+y/8vdMm9G+cnAztx OzW1WzclIcxdFQjDyZBoz9P+9KoWTnmVgPif0xuqsvT3oWq7uku44jmx4FhPTUQr 5D09D/YZoMkukUk0Re+HBsBrhRRVLZJDLPv2NVT+rV3/tB7b/CxMdiHLmqMhesp/ 1qwtMSUEex8VvI4rrstpTZgx07R2+cFKSo3BInOXNpLuOUMpXa8277gBCC5Wl0w= =E+wS -----END PGP SIGNATURE----- --=-=-=--