emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Rainer M Krug <Rainer@krugs.de>
To: emacs-orgmode@gnu.org
Cc: schulte.eric@gmail.com
Subject: [babel][PATCHES] ob-R patches for review
Date: Tue, 29 Apr 2014 14:43:29 +0200	[thread overview]
Message-ID: <m2fvkw9uha.fsf@krugs.de> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 625 bytes --]

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

-- 
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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: 0001-ob-R.el-Write-org-variables-into-own-R-environment.patch --]
[-- Type: text/x-patch, Size: 3528 bytes --]

From b1199e03758f42fde2f75f02a4c18cd71fac5a03 Mon Sep 17 00:00:00 2001
From: "Rainer M. Krug" <R.M.Krug@gmail.com>
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.
---
 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
--- 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))))
-             (org-babel-variable-assignments:R params)
-             (list body)
+	     '("try(detach(org), silent=TRUE)")
+             '("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-block'."
 			  "TRUE" "FALSE"))
 	      (row-names (if rownames-p "1" "NULL")))
 	  (if (= max min)
-	      (format "%s <- read.table(\"%s\",
-                      header=%s,
-                      row.names=%s,
-                      sep=\"\\t\",
-                      as.is=TRUE)" name file header row-names)
-	    (format "%s <- read.table(\"%s\",
-                   header=%s,
-                   row.names=%s,
-                   sep=\"\\t\",
-                   as.is=TRUE,
-                   fill=TRUE,
-                   col.names = paste(\"V\", seq_len(%d), sep =\"\"))"
-		    name file header row-names max))))
-    (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
+	      (format "assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE ), envir = org ); lockBinding('%s', org)" name file header row-names name)
+	    (format "assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE, fill=TRUE, col.names = paste(\"V\", seq_len(%d), sep =\"\") ), envir = org ); lockBinding('%s', org)" name file header row-names max name))))
+    (format "assign('%s', %s, envir = org); lockBinding('%s', org)" name (org-babel-R-quote-tsv-field value) name)))
 
 (defvar ess-ask-for-ess-directory) ; dynamically scoped
 (defun org-babel-R-initiate-session (session params)
-- 
1.8.5.2 (Apple Git-48)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.3: 0002-Add-saving-of-org-variable-document.patch --]
[-- Type: text/x-patch, Size: 874 bytes --]

From 9b1aed4c855fd54caa7b91e316b26ef7a813b7b0 Mon Sep 17 00:00:00 2001
From: "Rainer M. Krug" <R.M.Krug@gmail.com>
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.
---
 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
--- 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='orgVARIABLES.RData')")
 	     (list body)
              (when (cdr (assoc :epilogue params))
                (list (cdr (assoc :epilogue params)))))))
-- 
1.8.5.2 (Apple Git-48)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.4: 0003-Change-R-environment-name-from-org-to-org_variables_.patch --]
[-- Type: text/x-patch, Size: 2682 bytes --]

From 27a9daaf3b94c23b437d55c5d1e3d282139ced22 Mon Sep 17 00:00:00 2001
From: "Rainer M. Krug" <R.M.Krug@gmail.com>
Date: Wed, 26 Mar 2014 17:09:15 +0100
Subject: [PATCH 3/8] Change R environment name from org to org_variables_

---
 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
--- 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))))
-	     '("try(detach(org), silent=TRUE)")
-             '("org <- new.env()")
+	     '("try(detach(org_variables_), silent=TRUE)")
+             '("org_variables_ <- new.env()")
 	     (org-babel-variable-assignments:R params)
-	     '("lockEnvironment(org)")
-	     '("attach(org)")
-	     '("save(org, file='orgVARIABLES.RData')")
+	     '("lockEnvironment(org_variables_)")
+	     '("attach(org_variables_)")
+	     '("save(org_variables_, file='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 (= max min)
-	      (format "assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE ), envir = org ); lockBinding('%s', org)" name file header row-names name)
-	    (format "assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE, fill=TRUE, col.names = paste(\"V\", seq_len(%d), sep =\"\") ), envir = org ); lockBinding('%s', org)" name file header row-names max name))))
-    (format "assign('%s', %s, envir = org); lockBinding('%s', org)" name (org-babel-R-quote-tsv-field value) name)))
+	      (format "assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE ), envir = org_variables_ ); lockBinding('%s', org_variables_)" name file header row-names name)
+	    (format "assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE, fill=TRUE, col.names = paste(\"V\", seq_len(%d), sep =\"\") ), envir = org_variables_ ); lockBinding('%s', org_variables_)" name file header row-names max name))))
+    (format "assign('%s', %s, envir = org_variables_); lockBinding('%s', org_variables_)" name (org-babel-R-quote-tsv-field value) name)))
 
 (defvar ess-ask-for-ess-directory) ; dynamically scoped
 (defun org-babel-R-initiate-session (session params)
-- 
1.8.5.2 (Apple Git-48)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.5: 0004-Rename-org_variables-to-.org_variables.patch --]
[-- Type: text/x-patch, Size: 3060 bytes --]

From e8c977e0b38eb7eb64ed29f55bd985ca2e135c4c Mon Sep 17 00:00:00 2001
From: "Rainer M. Krug" <R.M.Krug@gmail.com>
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=ls()'
---
 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
--- 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))))
-	     '("try(detach(org_variables_), silent=TRUE)")
-             '("org_variables_ <- new.env()")
+	     '("try(detach(.org_variables_), silent=TRUE)")
+             '(".org_variables_ <- new.env()")
 	     (org-babel-variable-assignments:R params)
-	     '("lockEnvironment(org_variables_)")
-	     '("attach(org_variables_)")
-	     '("save(org_variables_, file='org_variables.RData')")
+	     '("lockEnvironment(.org_variables_)")
+	     '("attach(.org_variables_)")
+	     '("save(.org_variables_, file='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 (= max min)
-	      (format "assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE ), envir = org_variables_ ); lockBinding('%s', org_variables_)" name file header row-names name)
-	    (format "assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE, fill=TRUE, col.names = paste(\"V\", seq_len(%d), sep =\"\") ), envir = org_variables_ ); lockBinding('%s', org_variables_)" name file header row-names max name))))
-    (format "assign('%s', %s, envir = org_variables_); lockBinding('%s', org_variables_)" name (org-babel-R-quote-tsv-field value) name)))
+	      (format "assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE ), envir = .org_variables_ ); lockBinding('%s', .org_variables_)" name file header row-names name)
+	    (format "assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE, fill=TRUE, col.names = paste(\"V\", seq_len(%d), sep =\"\") ), envir = .org_variables_ ); lockBinding('%s', .org_variables_)" name file header row-names max name))))
+    (format "assign('%s', %s, envir = .org_variables_); lockBinding('%s', .org_variables_)" name (org-babel-R-quote-tsv-field value) name)))
 
 (defvar ess-ask-for-ess-directory) ; dynamically scoped
 (defun org-babel-R-initiate-session (session params)
-- 
1.8.5.2 (Apple Git-48)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.6: 0005-Adds-visual-separators.patch --]
[-- Type: text/x-patch, Size: 1421 bytes --]

From ba532e45ba7fe373effbeef3781e744b33f532a4 Mon Sep 17 00:00:00 2001
From: "Rainer M. Krug" <R.M.Krug@gmail.com>
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 blocks.
---
 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
--- 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=TRUE)")
              '(".org_variables_ <- new.env()")
 	     (org-babel-variable-assignments:R params)
 	     '("lockEnvironment(.org_variables_)")
 	     '("attach(.org_variables_)")
 	     '("save(.org_variables_, file='org_variables.RData')")
+	     '("## end org variable transfer")
+	     '("######################################################################")
 	     (list body)
              (when (cdr (assoc :epilogue params))
                (list (cdr (assoc :epilogue params)))))))
-- 
1.8.5.2 (Apple Git-48)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.7: 0006-Make-R-code-look-nicer-in-R.patch --]
[-- Type: text/x-patch, Size: 3013 bytes --]

From b3ddb3bf0bb327187acdd898a6173d22ceefe5fb Mon Sep 17 00:00:00 2001
From: "Rainer M. Krug" <R.M.Krug@gmail.com>
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.
---
 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
--- 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")
-	     '("try(detach(.org_variables_), silent=TRUE)")
-             '(".org_variables_ <- new.env()")
+	     '("  try(detach(.org_variables_), silent=TRUE)")
+             '("  .org_variables_ <- new.env()")
 	     (org-babel-variable-assignments:R params)
-	     '("lockEnvironment(.org_variables_)")
-	     '("attach(.org_variables_)")
-	     '("save(.org_variables_, file='org_variables.RData')")
+	     '("  lockEnvironment(.org_variables_)")
+	     '("  attach(.org_variables_)")
+	     '("  save(.org_variables_, file='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 (= max min)
-	      (format "assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE ), envir = .org_variables_ ); lockBinding('%s', .org_variables_)" name file header row-names name)
-	    (format "assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE, fill=TRUE, col.names = paste(\"V\", seq_len(%d), sep =\"\") ), envir = .org_variables_ ); lockBinding('%s', .org_variables_)" name file header row-names max name))))
-    (format "assign('%s', %s, envir = .org_variables_); lockBinding('%s', .org_variables_)" name (org-babel-R-quote-tsv-field value) name)))
+	      (format "    assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE ), envir = .org_variables_ ); lockBinding('%s', .org_variables_)" name file header row-names name)
+	    (format "    assign( '%s', read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE, fill=TRUE, col.names = paste(\"V\", seq_len(%d), sep =\"\") ), envir = .org_variables_ ); lockBinding('%s', .org_variables_)" name file header row-names max name))))
+    (format "    assign('%s', %s, envir = .org_variables_); lockBinding('%s', .org_variables_)" name (org-babel-R-quote-tsv-field value) name)))
 
 (defvar ess-ask-for-ess-directory) ; dynamically scoped
 (defun org-babel-R-initiate-session (session params)
-- 
1.8.5.2 (Apple Git-48)


[-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]

             reply	other threads:[~2014-04-29 12:45 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-29 12:43 Rainer M Krug [this message]
2014-04-29 18:15 ` [babel][PATCHES] ob-R patches for review Charles Berry
2014-04-30 12:28   ` Rainer M Krug
2014-04-30 22:49     ` Charles C. Berry
2014-05-01  9:10       ` Rainer M Krug
2014-05-07 10:27 ` Eric Schulte
2014-05-08  2:26   ` Charles Berry
2014-05-08 10:02     ` Rainer M Krug
2014-05-09  9:11       ` Rainer M Krug
2014-05-09 12:02         ` Rainer M Krug
2014-05-08  9:57   ` Rainer M Krug
2014-05-09 13:03     ` Bastien
2014-05-09 13:45       ` Rainer M Krug
2014-05-09 14:34         ` Eric Schulte
2014-05-12  8:33           ` Rainer M Krug
2014-05-12 12:23             ` Suvayu Ali
2014-05-12 12:41               ` Rainer M Krug
2014-05-12 14:01             ` Queestion concerning lists - was: " Rainer M Krug
2014-05-12 15:23               ` Eric Schulte
2014-05-12 15:21             ` Eric Schulte
2014-05-12 19:08               ` Rainer M Krug
2014-05-12 22:05                 ` Charles C. Berry
     [not found]                   ` <m2y4y2f499.fsf@krugs.de>
2014-05-16 18:22                     ` Charles C. Berry
2014-06-06 16:11                 ` Eric Schulte

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2fvkw9uha.fsf@krugs.de \
    --to=rainer@krugs.de \
    --cc=emacs-orgmode@gnu.org \
    --cc=schulte.eric@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).