From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rainer M Krug Subject: Re: [BUG][babel] Tangling into directories does not add directories to org file Date: Fri, 10 Oct 2014 10:24:05 +0200 Message-ID: References: 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]:51238) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XcVUx-0001h3-6l for emacs-orgmode@gnu.org; Fri, 10 Oct 2014 04:24:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XcVUr-0003QK-JW for emacs-orgmode@gnu.org; Fri, 10 Oct 2014 04:24:27 -0400 Received: from mail-wi0-f180.google.com ([209.85.212.180]:61184) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XcVUr-0003Pb-AV for emacs-orgmode@gnu.org; Fri, 10 Oct 2014 04:24:21 -0400 Received: by mail-wi0-f180.google.com with SMTP id em10so1244463wid.13 for ; Fri, 10 Oct 2014 01:24:20 -0700 (PDT) In-Reply-To: (Grant Rettke's message of "Thu, 9 Oct 2014 21:13:44 -0500") 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: Grant Rettke Cc: "emacs-orgmode@gnu.org" --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Grant Rettke writes: > Sounds like a valuable use of R. It definitely is. > > Do you have a writeup on the approach or is it a standard feature of > org and ess? No - but please bug me - I can post in the intermediate future an example demonstrating the points. But the main points are: * Setup ** I added the following to my emacs.org *** Add local variable in tangled files if they match [.]R The variable =3Dorg-babel-tangled-file t=3D enables ess to trace back the source from the .R file to the .org file, using the comments which have to include the org links. See the thread "Automatically adding local variables to tangled file" for d= etails #+begin_src emacs-lisp (defvar org-babel-tangled-file nil "If non-nill, current file was tangled with org-babel-tangle") (put 'org-babel-tangled-file 'safe-local-variable 'booleanp) (defun org-babel-mark-file-as-tangled () (when (buffer-file-name) (when (string-match "[.]R" (buffer-file-name)) (add-file-local-variable 'org-babel-tangled-file t) (add-file-local-variable 'buffer-read-only t) (add-file-local-variable 'eval: '(auto-revert-mode)) (basic-save-buffer)))) (add-hook 'org-babel-post-tangle-hook 'org-babel-mark-file-as-tangled) #+end_src ** Tangling =2D The files have to be tangled with the header argument =3D:comments link= =3D (or both) for this to work. =2D =3D:padline no=3D is necessary (at least it was) as the empty lines in the tangled file caused ess to identify the wrong line in the source code ** This should now work. * Use code as "package" Effectively, I do the following: ** code tangle all .R files to ./R *without variables transfer*. These should contain definitions of functions and not executable code.=20 ** data data files (i.e. the variables transferred from org) are transfered as tangled files in in the ./data directory. *** Species names and espre codes of species used by Vallet :PROPERTIES: :header-args+: :tangle ./data/SPECIESVALLET.R :header-args+: :rownames yes :header-args+: :colnames yes :END: #+NAME: SPECIESVALLET | | fullName | shortName | espre | IFNName | color | |---------+-----------------+-----------+---------+-----------------+------= -| | fagus | Fagus sylvatica | fagus | 4 | fagus_sylvatica | red = | | quercus | Quercus robur | quercus | 3 | quercus_robur | green= | #+begin_src R :var SPECIESVALLET=3DSPECIESVALLET #+end_src which evaluates to ./data/SPECIESVALLET.R ,---- | ## Species names and espre codes of species used by Vallet | ## :PROPERTIES: | ## :header-args+: :tangle ./data/SPECIESVALLET.R | ## :header-args+: :rownames yes | ## :header-args+: :colnames yes | ## :END: | ## #+NAME: SPECIESVALLET | ## | | fullName | shortName | espre | IFNName | co= lor | | ## |---------+-----------------+-----------+---------+-----------------+-= ------| | ## | fagus | Fagus sylvatica | fagus | 4 | fagus_sylvatica | = red | | ## | quercus | Quercus robur | quercus | 3 | quercus_robur | = green | |=20 | ## [[file:prodMixStands.org::*Species%20names%20and%20espre%20codes%20of%= 20species%20used%20by%20Vallet][Species\ names\ and\ espre\ codes\ of\ spec= ies\ used\ by\ Vallet:1]] | SPECIESVALLET <- local({ | con <- textConnection( | "\"fullName\" \"shortName\" \"espre\" \"IFNName\" \"color\" | \"fagus\" \"Fagus sylvatica\" \"fagus\" \"4\" \"fagus_sylvatica\" \"red\" | \"quercus\" \"Quercus robur\" \"quercus\" \"3\" \"quercus_robur\" \"green= \"" | ) | res <- utils::read.table( | con, | header =3D TRUE, | row.names =3D 1, | sep =3D "\t", | as.is =3D TRUE | ) | close(con) | res | }) | ## Species\ names\ and\ espre\ codes\ of\ species\ used\ by\ Vallet:1 end= s here |=20 | ## Local Variables: | ## org-babel-tangled-file: t | ## buffer-read-only: t | ## eval:: (auto-revert-mode) | ## End: `---- ** evaluation data and code is *never* evaluated, i.e.=20 :PROPERTIES: :header-args+: :eval never :END: The code and data is loaded as follow: #+begin_src R=20 library(devtools) load_all("./") #+end_src and there you go. =20 To debug the code, I enable developer mode in ESS and add the package =3Dthe_name_of_the_directory_./=3D to be debugged. * Using the code I have a second subtree (except of the one containing the code and the data) which is evaluated (as some lengthy analysis are involved, not on exp= ort but before) and contains the actual analysis.=20 * Advantages =2D All code and data is enclosed in a "package" environment and, =2D when the project is finished, it can be easily packed in a package and handed on to other users. =2D documentation to the functions can be added using roxygen which can easily be translated into .rd files * Final words I hope this clarifies my approach. For me, this is a very "natural" approach, as I like to use packages and capsulate everything in environments / packages, and it seems very "clean" to me to define the core of the analysis as functions in =3D./R/*.R=3D files, the variables in =3D./data/*=3D files and the actual analysis as mainly a few function calls. If you have any further questions, please go on and ask. Cheers, Rainer > > Online Thu, Oct 9, 2014 at 7:37 AM, Rainer M Krug wrote: >> >> I'd like to ping this as I think it >> is an important bug. It e.g. make debugging of R packages, whose source >> is in org, more difficult, (the .R files are tangled into ./R/) as ess >> does not find the source of the R code in R as it uses the links in the >> comments to find the org file. >> >> Temporary solution: I created a link to the org file in the ./R director= y. >> >> Cheers, >> >> Rainer >> >> Rainer M Krug writes: >> >>> When in the tangling taerget a directory is specified, this is not >>> reflected in the comments in the tangled file when comments are set to >>> include links: >>> >>> ,---- >>> | * `link' The code block is wrapped in comments which contain >>> | pointers back to the original Org file from which the code was >>> | tangled. >>> `---- >>> >>> In the org file =3DprodMixStands.org=3D: >>> >>> ,---- >>> | :PROPERTIES: >>> | :header-args+: :tangle ./R/update.cache.R >>> | :END: >>> | #+begin_src R >>> | test >>> | #+end_src >>> `---- >>> >>> results in the file =3Dupdate.cache.R=3D in a link to the file >>> =3DprodMixStands.org=3D and not =3D./../prodMixstands.org=3D: >>> >>> ,---- >>> | ## [[file:prodMixStands.org::*Begin][Begin:1]] >>> `---- >>> >>> This is particularly bad when using ess and developer mode to develop a >>> package. >>> >>> Cheers, >>> >>> Rainer >> >> -- >> Rainer M. Krug >> email: Rainerkrugsde >> PGP: 0x0F52F982 >> =2D-=20 Rainer M. Krug email: Rainerkrugsde PGP: 0x0F52F982 --=-=-= Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.22 (Darwin) iQEcBAEBAgAGBQJUN5etAAoJENvXNx4PUvmCYfwIAK5haKbNPOupVQITz6GYrcn2 ycjRRIR+tse8dKj4iX2p8GaZ/z+bvGz1n9FA9xwsr/NddgPLOO8Ei+8JRmBnDofJ 2eIH0cYLcvXqI+/O02XPAlg9jtu5cuWpo03MR5N02TZc8ffmbTetBXRXQ64GPttH qpoxYJHNM53g1diSD3UoOhDVuKDvngU3tifuXYgx9RtDNsaJyDKSCYIvkK/5xmzL QAF1oZztBBJ/HZSomrP50kB76QSh4/LF+mCFekRRbzu5q/4pAXhKsi73BcBBOKOt bQM382DCGEN3tytQvOTWA1vF6pgqUR8xf5PXnT8d4wsdFSk2oiYFkxj4XsUJDW8= =Jk8/ -----END PGP SIGNATURE----- --=-=-=--