From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rainer M Krug Subject: [babel][PATCHES] ob-R patches for review Date: Tue, 29 Apr 2014 14:43:29 +0200 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]:49187) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wf7P4-0005LY-NL for emacs-orgmode@gnu.org; Tue, 29 Apr 2014 08:45:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wf7Oh-0004Dr-1x for emacs-orgmode@gnu.org; Tue, 29 Apr 2014 08:44:54 -0400 Received: from mail-wi0-f179.google.com ([209.85.212.179]:36009) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wf7Og-0004Da-DE for emacs-orgmode@gnu.org; Tue, 29 Apr 2014 08:44:30 -0400 Received: by mail-wi0-f179.google.com with SMTP id z2so350400wiv.12 for ; Tue, 29 Apr 2014 05:44:29 -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 Cc: schulte.eric@gmail.com --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Hi Attached please find seven patches for review to implement the storing of org variables in their own environment and to make the org-issued R code look nicer in the R session. Thanks, Rainer =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-ob-R.el-Write-org-variables-into-own-R-environment.patch Content-Transfer-Encoding: quoted-printable From=20b1199e03758f42fde2f75f02a4c18cd71fac5a03 Mon Sep 17 00:00:00 2001 From: "Rainer M. Krug" Date: Thu, 20 Mar 2014 11:35:44 +0100 Subject: [PATCH 1/8] ob-R.el: Write org variables into own R environment * 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: text/x-patch Content-Disposition: attachment; filename=0002-Add-saving-of-org-variable-document.patch Content-Transfer-Encoding: quoted-printable From=209b1aed4c855fd54caa7b91e316b26ef7a813b7b0 Mon Sep 17 00:00:00 2001 From: "Rainer M. Krug" Date: Wed, 26 Mar 2014 16:53:29 +0100 Subject: [PATCH 2/8] Add saving of org variable document * lisp/ob-R.el (org-babel-expand-body:R): add R command to save `org' environment after variables are stored and locked. =2D-- lisp/ob-R.el | 1 + 1 file changed, 1 insertion(+) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index 82971de..af90e68 100644 =2D-- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -108,6 +108,7 @@ this variable.") (org-babel-variable-assignments:R params) '("lockEnvironment(org)") '("attach(org)") + '("save(org, file=3D'orgVARIABLES.RData')") (list body) (when (cdr (assoc :epilogue params)) (list (cdr (assoc :epilogue params))))))) =2D-=20 1.8.5.2 (Apple Git-48) --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0003-Change-R-environment-name-from-org-to-org_variables_.patch Content-Transfer-Encoding: quoted-printable From=2027a9daaf3b94c23b437d55c5d1e3d282139ced22 Mon Sep 17 00:00:00 2001 From: "Rainer M. Krug" Date: Wed, 26 Mar 2014 17:09:15 +0100 Subject: [PATCH 3/8] Change R environment name from org to org_variables_ =2D-- lisp/ob-R.el | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index af90e68..b51e582 100644 =2D-- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -103,12 +103,12 @@ this variable.") (append (when (cdr (assoc :prologue params)) (list (cdr (assoc :prologue params)))) =2D '("try(detach(org), silent=3DTRUE)") =2D '("org <- new.env()") + '("try(detach(org_variables_), silent=3DTRUE)") + '("org_variables_ <- new.env()") (org-babel-variable-assignments:R params) =2D '("lockEnvironment(org)") =2D '("attach(org)") =2D '("save(org, file=3D'orgVARIABLES.RData')") + '("lockEnvironment(org_variables_)") + '("attach(org_variables_)") + '("save(org_variables_, file=3D'org_variables.RData')") (list body) (when (cdr (assoc :epilogue params)) (list (cdr (assoc :epilogue params))))))) @@ -208,9 +208,9 @@ This function is called by `org-babel-execute-src-block= '." "TRUE" "FALSE")) (row-names (if rownames-p "1" "NULL"))) (if (=3D max min) =2D (format "assign( '%s', read.table(\"%s\", header=3D%s, row.names= =3D%s, sep=3D\"\\t\", as.is=3DTRUE ), envir =3D org ); lockBinding('%s', or= g)" name file header row-names name) =2D (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\", se= q_len(%d), sep =3D\"\") ), envir =3D org ); lockBinding('%s', org)" name fi= le header row-names max name)))) =2D (format "assign('%s', %s, envir =3D org); lockBinding('%s', org)" na= me (org-babel-R-quote-tsv-field value) name))) + (format "assign( '%s', read.table(\"%s\", header=3D%s, row.names=3D= %s, sep=3D\"\\t\", as.is=3DTRUE ), envir =3D org_variables_ ); lockBinding(= '%s', org_variables_)" 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_variables_ ); lockBinding('%s', org_= variables_)" name file header row-names max name)))) + (format "assign('%s', %s, envir =3D org_variables_); lockBinding('%s',= org_variables_)" 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: text/x-patch Content-Disposition: attachment; filename=0004-Rename-org_variables-to-.org_variables.patch Content-Transfer-Encoding: quoted-printable From=20e8c977e0b38eb7eb64ed29f55bd985ca2e135c4c Mon Sep 17 00:00:00 2001 From: "Rainer M. Krug" Date: Fri, 28 Mar 2014 12:13:53 +0100 Subject: [PATCH 4/8] Rename `org_variables' to `.org_variables' * lisp/ob-R.el (org-babel-R-assign-elisp, org-babel-expand-body:R): Change name of R environment from `org_variables' to `.org_variables'. The `.' makes the environment hidden in R and it can not be directly deleted in R by using `rm(list=3Dls()' =2D-- lisp/ob-R.el | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index b51e582..214c14d 100644 =2D-- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -103,12 +103,12 @@ this variable.") (append (when (cdr (assoc :prologue params)) (list (cdr (assoc :prologue params)))) =2D '("try(detach(org_variables_), silent=3DTRUE)") =2D '("org_variables_ <- new.env()") + '("try(detach(.org_variables_), silent=3DTRUE)") + '(".org_variables_ <- new.env()") (org-babel-variable-assignments:R params) =2D '("lockEnvironment(org_variables_)") =2D '("attach(org_variables_)") =2D '("save(org_variables_, file=3D'org_variables.RData')") + '("lockEnvironment(.org_variables_)") + '("attach(.org_variables_)") + '("save(.org_variables_, file=3D'org_variables.RData')") (list body) (when (cdr (assoc :epilogue params)) (list (cdr (assoc :epilogue params))))))) @@ -208,9 +208,9 @@ This function is called by `org-babel-execute-src-block= '." "TRUE" "FALSE")) (row-names (if rownames-p "1" "NULL"))) (if (=3D max min) =2D (format "assign( '%s', read.table(\"%s\", header=3D%s, row.names= =3D%s, sep=3D\"\\t\", as.is=3DTRUE ), envir =3D org_variables_ ); lockBindi= ng('%s', org_variables_)" name file header row-names name) =2D (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\", se= q_len(%d), sep =3D\"\") ), envir =3D org_variables_ ); lockBinding('%s', or= g_variables_)" name file header row-names max name)))) =2D (format "assign('%s', %s, envir =3D org_variables_); lockBinding('%s= ', org_variables_)" name (org-babel-R-quote-tsv-field value) name))) + (format "assign( '%s', read.table(\"%s\", header=3D%s, row.names=3D= %s, sep=3D\"\\t\", as.is=3DTRUE ), envir =3D .org_variables_ ); lockBinding= ('%s', .org_variables_)" 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_variables_ ); lockBinding('%s', .or= g_variables_)" name file header row-names max name)))) + (format "assign('%s', %s, envir =3D .org_variables_); lockBinding('%s'= , .org_variables_)" 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: text/x-patch Content-Disposition: attachment; filename=0005-Adds-visual-separators.patch Content-Transfer-Encoding: quoted-printable From=20ba532e45ba7fe373effbeef3781e744b33f532a4 Mon Sep 17 00:00:00 2001 From: "Rainer M. Krug" Date: Mon, 31 Mar 2014 16:57:14 +0200 Subject: [PATCH 5/8] Adds visual separators * ob-R.el (org-babel-expand-body:R): Add visual separators in R code to separate the automatically added code from the code from the code bloc= ks. =2D-- lisp/ob-R.el | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index 214c14d..3534580 100644 =2D-- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -103,12 +103,16 @@ this variable.") (append (when (cdr (assoc :prologue params)) (list (cdr (assoc :prologue params)))) + '("#################################################################= #####") + '("## Beginning org variable transfer") '("try(detach(.org_variables_), silent=3DTRUE)") '(".org_variables_ <- new.env()") (org-babel-variable-assignments:R params) '("lockEnvironment(.org_variables_)") '("attach(.org_variables_)") '("save(.org_variables_, file=3D'org_variables.RData')") + '("## end org variable transfer") + '("#################################################################= #####") (list body) (when (cdr (assoc :epilogue params)) (list (cdr (assoc :epilogue params))))))) =2D-=20 1.8.5.2 (Apple Git-48) --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0006-Make-R-code-look-nicer-in-R.patch Content-Transfer-Encoding: quoted-printable From=20b3ddb3bf0bb327187acdd898a6173d22ceefe5fb Mon Sep 17 00:00:00 2001 From: "Rainer M. Krug" Date: Mon, 7 Apr 2014 10:52:00 +0200 Subject: [PATCH 6/8] Make R code look nicer in R * lisp/ob-R.el (org-babel-expand-body:R): Added Space to the R code blocks to make them easier identifiable in R. =2D-- lisp/ob-R.el | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index 3534580..5edbed9 100644 =2D-- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -105,12 +105,12 @@ this variable.") (list (cdr (assoc :prologue params)))) '("#################################################################= #####") '("## Beginning org variable transfer") =2D '("try(detach(.org_variables_), silent=3DTRUE)") =2D '(".org_variables_ <- new.env()") + '(" try(detach(.org_variables_), silent=3DTRUE)") + '(" .org_variables_ <- new.env()") (org-babel-variable-assignments:R params) =2D '("lockEnvironment(.org_variables_)") =2D '("attach(.org_variables_)") =2D '("save(.org_variables_, file=3D'org_variables.RData')") + '(" lockEnvironment(.org_variables_)") + '(" attach(.org_variables_)") + '(" save(.org_variables_, file=3D'org_variables.RData')") '("## end org variable transfer") '("#################################################################= #####") (list body) @@ -212,9 +212,9 @@ This function is called by `org-babel-execute-src-block= '." "TRUE" "FALSE")) (row-names (if rownames-p "1" "NULL"))) (if (=3D max min) =2D (format "assign( '%s', read.table(\"%s\", header=3D%s, row.names= =3D%s, sep=3D\"\\t\", as.is=3DTRUE ), envir =3D .org_variables_ ); lockBind= ing('%s', .org_variables_)" name file header row-names name) =2D (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\", se= q_len(%d), sep =3D\"\") ), envir =3D .org_variables_ ); lockBinding('%s', .= org_variables_)" name file header row-names max name)))) =2D (format "assign('%s', %s, envir =3D .org_variables_); lockBinding('%= s', .org_variables_)" name (org-babel-R-quote-tsv-field value) name))) + (format " assign( '%s', read.table(\"%s\", header=3D%s, row.name= s=3D%s, sep=3D\"\\t\", as.is=3DTRUE ), envir =3D .org_variables_ ); lockBin= ding('%s', .org_variables_)" 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_variables_ ); lockBinding('%s'= , .org_variables_)" name file header row-names max name)))) + (format " assign('%s', %s, envir =3D .org_variables_); lockBinding(= '%s', .org_variables_)" 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) iQEcBAEBAgAGBQJTX55xAAoJENvXNx4PUvmC5SsH/3yrkTuGevvBCCfmmQTpGm+n nO7Z7D7qxL82EOLdvZZtxa3LOwsg/06NqLLemE9mvwS4zfmMUSuGVWlYsWLsXDP7 NbyObK/Kd+d93SScD4J8ZhbHQROiRO7veFrvcwcjqtuOopEtCZSxgKDLe43bBpeu vMPKy3rkFSIwdeD9Vo5oOqoogHDP+Ew534+BZ7Li2L7EtvcScDgIMzfG0HljbeDw Zen2MIEicA4SmBgsWfNCz+A4mHoUgNEaybSexzE89emFePrQHyUYUMOClC6ej7kA TLvkUQjfzGbmVfN3VAyGl4bgmJDTGG3mQgXQZdTkH1w8faPYh24oUr05QdDWNHQ= =afNW -----END PGP SIGNATURE----- --==-=-=--