emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Write org variables into own R environment instead of .GlobalEnv
@ 2014-03-18 14:05 Rainer M Krug
  2014-03-18 15:15 ` Bastien
  0 siblings, 1 reply; 10+ messages in thread
From: Rainer M Krug @ 2014-03-18 14:05 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 4119 bytes --]


* (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 =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 | 47 +++++++++++++++++++++++++++++++----------------
 1 file changed, 31 insertions(+), 16 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 62aa7f2..da2aea2 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,31 @@ 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)


-- 
Rainer M. Krug

email: RMKrug<at>gmail<dot>com

PGP: 0x0F52F982

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

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] Write org variables into own R environment instead of .GlobalEnv
  2014-03-18 14:05 [PATCH] Write org variables into own R environment instead of .GlobalEnv Rainer M Krug
@ 2014-03-18 15:15 ` Bastien
  2014-03-20  9:27   ` Rainer M Krug
  2014-03-20 11:02   ` [PATCH] Write org variables into own R environment Rainer M Krug
  0 siblings, 2 replies; 10+ messages in thread
From: Bastien @ 2014-03-18 15:15 UTC (permalink / raw)
  To: Rainer M Krug; +Cc: emacs-orgmode

Hi Rainer,

did you sign the FSF papers?  We need this before considering applying
the patch.  Also, the formatting of the Changelog entry is not correct,
please carefully read http://orgmode.org/worg/org-contribute.html

Thanks,

-- 
 Bastien

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Write org variables into own R environment instead of .GlobalEnv
  2014-03-18 15:15 ` Bastien
@ 2014-03-20  9:27   ` Rainer M Krug
  2014-03-20  9:53     ` Bastien
  2014-03-20 11:02   ` [PATCH] Write org variables into own R environment Rainer M Krug
  1 sibling, 1 reply; 10+ messages in thread
From: Rainer M Krug @ 2014-03-20  9:27 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 549 bytes --]

Bastien <bzg@gnu.org> writes:

> Hi Rainer,
>
> did you sign the FSF papers?  We need this before considering applying
> the patch.  

I mailed them on Tuesday, using the form
[[http://orgmode.org/request-assign-future.txt]] but did not get a
response.

> Also, the formatting of the Changelog entry is not correct,
> please carefully read http://orgmode.org/worg/org-contribute.html

OK - I will do so and re-send the patch.

Cheers,

Rainer

>
> Thanks,

-- 
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Write org variables into own R environment instead of .GlobalEnv
  2014-03-20  9:27   ` Rainer M Krug
@ 2014-03-20  9:53     ` Bastien
  2014-03-20 11:03       ` Rainer M Krug
  0 siblings, 1 reply; 10+ messages in thread
From: Bastien @ 2014-03-20  9:53 UTC (permalink / raw)
  To: Rainer M Krug; +Cc: emacs-orgmode

Hi Rainer,

Rainer M Krug <Rainer@krugs.de> writes:

> I mailed them on Tuesday, using the form
> [[http://orgmode.org/request-assign-future.txt]] but did not get a
> response.

This usually take 4-6 weeks, so let's be patient.

>> Also, the formatting of the Changelog entry is not correct,
>> please carefully read http://orgmode.org/worg/org-contribute.html
>
> OK - I will do so and re-send the patch.

Thanks in advance!

-- 
 Bastien

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH] Write org variables into own R environment
  2014-03-18 15:15 ` Bastien
  2014-03-20  9:27   ` Rainer M Krug
@ 2014-03-20 11:02   ` Rainer M Krug
  2014-03-26  5:06     ` Aaron Ecay
  1 sibling, 1 reply; 10+ messages in thread
From: Rainer M Krug @ 2014-03-20 11:02 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 3296 bytes --]


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

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

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] Write org variables into own R environment instead of .GlobalEnv
  2014-03-20  9:53     ` Bastien
@ 2014-03-20 11:03       ` Rainer M Krug
  2014-03-20 11:06         ` Bastien
  0 siblings, 1 reply; 10+ messages in thread
From: Rainer M Krug @ 2014-03-20 11:03 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 714 bytes --]

Bastien <bzg@altern.org> writes:

> Hi Rainer,
>
> Rainer M Krug <Rainer@krugs.de> writes:
>
>> I mailed them on Tuesday, using the form
>> [[http://orgmode.org/request-assign-future.txt]] but did not get a
>> response.
>
> This usually take 4-6 weeks, so let's be patient.

OK - I'll let you know when I hear something.

>
>>> Also, the formatting of the Changelog entry is not correct,
>>> please carefully read http://orgmode.org/worg/org-contribute.html
>>
>> OK - I will do so and re-send the patch.
>
> Thanks in advance!

Patch is in a separate email - hope the commit message is better now.

Thanks,

Rainer

-- 
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Write org variables into own R environment instead of .GlobalEnv
  2014-03-20 11:03       ` Rainer M Krug
@ 2014-03-20 11:06         ` Bastien
  2014-03-20 11:16           ` Rainer M Krug
  0 siblings, 1 reply; 10+ messages in thread
From: Bastien @ 2014-03-20 11:06 UTC (permalink / raw)
  To: Rainer M Krug; +Cc: emacs-orgmode

Rainer M Krug <Rainer@krugs.de> writes:

> OK - I'll let you know when I hear something.

We should both hear about it.

Please ping me if you don't hear anything in a month,
I'll then ask again to the FSF copyright clerk.

>>>> Also, the formatting of the Changelog entry is not correct,
>>>> please carefully read http://orgmode.org/worg/org-contribute.html
>>>
>>> OK - I will do so and re-send the patch.
>>
>> Thanks in advance!
>
> Patch is in a separate email - hope the commit message is better
> now.

Looks good, thanks!

-- 
 Bastien

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Write org variables into own R environment instead of .GlobalEnv
  2014-03-20 11:06         ` Bastien
@ 2014-03-20 11:16           ` Rainer M Krug
  0 siblings, 0 replies; 10+ messages in thread
From: Rainer M Krug @ 2014-03-20 11:16 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 724 bytes --]

Bastien <bzg@gnu.org> writes:

> Rainer M Krug <Rainer@krugs.de> writes:
>
>> OK - I'll let you know when I hear something.
>
> We should both hear about it.
>
> Please ping me if you don't hear anything in a month,
> I'll then ask again to the FSF copyright clerk.

I'll do - thanks.

>
>>>>> Also, the formatting of the Changelog entry is not correct,
>>>>> please carefully read http://orgmode.org/worg/org-contribute.html
>>>>
>>>> OK - I will do so and re-send the patch.
>>>
>>> Thanks in advance!
>>
>> Patch is in a separate email - hope the commit message is better
>> now.
>
> Looks good, thanks!

Thanks,

Rainer

-- 
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Write org variables into own R environment
  2014-03-20 11:02   ` [PATCH] Write org variables into own R environment Rainer M Krug
@ 2014-03-26  5:06     ` Aaron Ecay
  2014-03-26 15:52       ` Rainer M Krug
  0 siblings, 1 reply; 10+ messages in thread
From: Aaron Ecay @ 2014-03-26  5:06 UTC (permalink / raw)
  To: Rainer M Krug, Bastien; +Cc: emacs-orgmode

Hi Rainer,

This patch looks very promising.  (I have not had a chance to test the
implementation yet).  One issue might be the name of the “org” variable
– maybe this should instead be “.org” or “_org” to lessen the chance of
stomping on pre-existing variables named “org” in people’s code.

Thanks (also for your previous patch for handling in graphics blocks),

--
Aaron Ecay

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] Write org variables into own R environment
  2014-03-26  5:06     ` Aaron Ecay
@ 2014-03-26 15:52       ` Rainer M Krug
  0 siblings, 0 replies; 10+ messages in thread
From: Rainer M Krug @ 2014-03-26 15:52 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1482 bytes --]

Aaron Ecay <aaronecay@gmail.com> writes:

> Hi Rainer,
>
> This patch looks very promising.  (I have not had a chance to test the
> implementation yet).  

Thanks - please let me know as soon as you tested it.

> One issue might be the name of the “org” variable
> – maybe this should instead be “.org” or “_org” to lessen the chance of
> stomping on pre-existing variables named “org” in people’s code.

Agreed here. .org would be a tricky solution, because it is hidden -
advantage or disadvantage? Probably a disadvantage. I will rename it to 
"_org_variables_" which is likely to be unique.

Do you think it would be worth to have a check along the lines of 

if(class(_org_variables_)!="environment") {
  stop("_org_variables_ is already defined ")
} else {
  ...
}

or to make the name user definable?

A different thing which I am using at the moment is to save the
environment so that it can be easily distributed along the tangled code
*if all variables are defined at the beginning and not per code block*.

Another option I am thinking about is to provide the possibility to
store not only variables, but also R function - useful? Effectively a
kind of "R package light for org-mode"?

>
> Thanks (also for your previous patch for handling in graphics blocks),

Pleasure - Thanks that they are useful.

Rainer
>
> --
> Aaron Ecay
>
>

-- 
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2014-03-26 15:52 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-18 14:05 [PATCH] Write org variables into own R environment instead of .GlobalEnv Rainer M Krug
2014-03-18 15:15 ` Bastien
2014-03-20  9:27   ` Rainer M Krug
2014-03-20  9:53     ` Bastien
2014-03-20 11:03       ` Rainer M Krug
2014-03-20 11:06         ` Bastien
2014-03-20 11:16           ` Rainer M Krug
2014-03-20 11:02   ` [PATCH] Write org variables into own R environment Rainer M Krug
2014-03-26  5:06     ` Aaron Ecay
2014-03-26 15:52       ` Rainer M Krug

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