From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rainer M Krug Subject: [PATCH] Write org variables into own R environment Date: Thu, 20 Mar 2014 12:02:01 +0100 Message-ID: References: <8738ifh70y.fsf@bzg.ath.cx> 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]:55571) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WQak1-0007um-4d for emacs-orgmode@gnu.org; Thu, 20 Mar 2014 07:02:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WQajg-0003mf-DU for emacs-orgmode@gnu.org; Thu, 20 Mar 2014 07:02:29 -0400 In-Reply-To: <8738ifh70y.fsf@bzg.ath.cx> (Bastien's message of "Tue, 18 Mar 2014 16:15:57 +0100") 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: Bastien Cc: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable * lisp/ob-R.el (org-babel-expand-body:R): Create empty environment called `org' in R before adding variables and afterwards lock it and add it to the R search path. (org-babel-variable-assignments:R): Assign variables into own `org' environment in R instead of .GlobalEnv These patch implements the writing of org variables into a separate R environment and attaches it to the search path. For the usage of these variables, nothing changes in R, but: 1) The org variables are now grouped and can be seen via `ls(org)' in R and are not shown anymore in the .GlobalEnv when using `ls()' 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 restored 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 | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index 62aa7f2..82971de 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,9 @@ This function is called by `org-babel-execute-src-bloc= k'." "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', 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', 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 max 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) --=-=-= Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.22 (Darwin) iQEcBAEBAgAGBQJTKsqtAAoJENvXNx4PUvmChYEH/26rj/bF7LrWn1NbmfSmcB25 IaUgf9X+VlGR94mBIGQuqOlJveysUY5m8e2euJ4c/Qo+0x01Rmw4W+TVdphfC5rx oR+wVYwa0CPv+Ur6AgnETa37iJ3skgLwizJm1GZMBprWO1QczS4M223XK9eJwvqI 9SlaXj5nmlMEk1Tu0WUE6WfFmOQbMNCVdNwyhpE4FCuEnc/ut/pB2V6PB9SeAv7P Y6TMWVXu08sAH/OEFB5u1FuoDH2izUiy0nFO7F5CklVzFgjwjHuOLo2zlQK2gutT bFr5zycQlx0eV4b9nu7PkzMUKJsuVo+qjihLIHFnpl0EpO8pTLYUgkcNR59ISdM= =9kB7 -----END PGP SIGNATURE----- --=-=-=--