From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rainer M Krug Subject: Tangling with variables in R Date: Mon, 02 Jun 2014 10:44:21 +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]:56977) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WrNrA-0001cM-79 for emacs-orgmode@gnu.org; Mon, 02 Jun 2014 04:44:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WrNr4-0000pM-RL for emacs-orgmode@gnu.org; Mon, 02 Jun 2014 04:44:36 -0400 Received: from mail-we0-f180.google.com ([74.125.82.180]:33068) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WrNr4-0000oy-Id for emacs-orgmode@gnu.org; Mon, 02 Jun 2014 04:44:30 -0400 Received: by mail-we0-f180.google.com with SMTP id q58so4820026wes.11 for ; Mon, 02 Jun 2014 01:44:29 -0700 (PDT) Received: from Rainers-MacBook-Pro-3.local (arn78-1-88-186-171-7.fbx.proxad.net. [88.186.171.7]) by mx.google.com with ESMTPSA id ej2sm33670381wjd.21.2014.06.02.01.44.28 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 02 Jun 2014 01:44:28 -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 Hi I just realized (again) that tangling with variables in R contains many particularities. 1) it only works with non-tables, i.e. single values. When defining the following variables: =2D-8<---------------cut here---------------start------------->8--- #+NAME: YEARS | | year | |---+---------------| | 1 | 1990 | | 2 | 2000 | #+PROPERTY: var+ YEARS=3DYEARS #+PROPERTY: var+ PRESENT=3D2008 =2D-8<---------------cut here---------------end--------------->8--- I get the following code in the tangled .R file: ,---- | YEARS <- read.table("/var/folders/50/wcr5bjwn75q595n6x82gxj280000gn/T/bab= el-97151aBD/R-import-97151vpn", | header=3DTRUE, | row.names=3D1, | sep=3D"\t", | as.is=3DTRUE) | PRESENT <- 2008 `---- Variable transfer from tables does not work, as it is based on a temporary file, which is not easy to distribute together with the tangled.R file and requires manual work as the path will be different. I consider this as a bug which should be fixed. 2) With regards to variables which are defined non-file wide, (i.e. in properties in a subtree and variables defined per code block and function call), these are set when they occur in the tangled code, but they are not unset *for the next code block* or *for R code in the next subtree* (I hope you know what I mean). They are therefore similar to the use of a session header argument instead of non-session evaluation of the code. This is a (conceptually) a more complex issue, and requires some initial thought on how this should be dealt with and how the tangled code should relate to the executed code. =2D Should the tangled .R file result in the same output as the execution in the org file, i.e. an accompanying .R file to a exported pdf, so that the .R file can be used to reproduce the graphs and analysis in the .pdf exported from the .org? or =2D Is tangling a completely thing to execution, and the resulting R code in the .R file is not expected to reproduce the results in the org file? =2D Finally, does tangling with variables makes sense? My opinions are=20 a) *All* variables should be transferred to the .R file. This can be already disabled via the :no-expand header argument. Unfortunately, this is combined with noweb expansion, and it might be useful to split these two, i.e. to be able to only disable variable expansion but to keep noweb (I don't use noweb so far, so it is not a problem to me as it is now). b) The variable assignments should be per code block / function call. So a tangled block should look as follow: =2D-8<---------------cut here---------------start------------->8--- #+NAME: YEARS | | year | |---+---------------| | 1 | 1990 | | 2 | 2000 | #+PROPERTY: var+ YEARS=3DYEARS #+PROPERTY: var+ PRESENT=3D2008 #+begin_src R x <- 4 cat(x^2) #+end_src =2D-8<---------------cut here---------------end--------------->8--- should result in something like the following: ,---- | ## # Set the variables | YEARS <- TRANSFER_TABLE() | PRESENT <- TRANSFER_VALUE() | ## Here begins the real code | x <- 4 | cat(x^2) | ## # Unset all variables previously set `---- but I prefer the following approach, as the result would be very similar, only that the variables are still present afterwards which would make debugging easier: ,---- | ## # Unset all variables previously set | ## # Set the variables | YEARS <- TRANSFER_TABLE() | PRESENT <- TRANSFER_VALUE() | ## Here begins the real code | x <- 4 | cat(x^2) `---- This is effectively already implemented by using R environments. See [1] and particularly [2] and [3] for how it is implemented. This does not yet address the concern about the transfer of tables, but I will look at this. Apologies for a long post, but I would like to know which direction of the tangling / variable transfer from org to R should take - I don't want to spend a lot of time solving a problem which does not really exist. So - any comments? Suggestions? Thanks, Rainer Footnotes:=20 [1] https://github.com/rkrug/orgmode-dev [2] https://github.com/rkrug/orgmode-dev/blob/R-env/lisp/ob-R.el [3] https://github.com/rkrug/orgmode-dev/blob/R-env/etc/R/org_functions.R =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: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.22 (Darwin) iQEcBAEBAgAGBQJTjDlqAAoJENvXNx4PUvmCAM0IAMrdsuvIPvSzNMzqubm9s3kc rrTjmcPs4RvM5BYQKpXg+MO+S4Mlvykmb1nsNc7HBfbFuiF57kG3zSQmNv8tEq1C mt20IxsWxg4Y+Zz3wmi+U9s54EKfrXe7g+oE7BlSDxET9bnN4mjP6KS52bFocWK9 XxedI/87jtTW2abnSEO+OXkoRNKi9O7Bp+3+bDwCqVM8yxsYDj9tBq1XRxnByWmN n8jBnCz3ldH8oIOnhy5k79ZBnkRPd8yrlwNLyP5lkvUcWScD6YTmoBe8YKN6mYbw 6IIuTiTpCuPxZbB5nNU35W7Jg1NHravdT1PaMoSM89TENcMXO2GyiCYh7ABNRhM= =bB5q -----END PGP SIGNATURE----- --=-=-=--