I'm starting using org-mode
and org-babel
to support my day by day scientific work and produce reproductibles papers.
Lot of people use rMarkdown
/jupyter
to do that, but i'm interested by investing some time to do more or less the same things using org-babel
and org-mode
.
Running chunk of code using org-babel is as simple as running chunk of code with RMarkdown/KnitR, and it seems org-babel is much powerfull on this side, so no problem with that actually
For me, problem arrives when i try to manage path, and start using :session
property, especially with shell (sh/bash).
I define the workspace :dir for source code like this :
:PROPERTIES:
:export_file_name: reproductiblepaper
:header-args: :dir ~/home/xxx/reproductible_paper tangle:yes session: reproductible
:END:
It's common to use git or any cvs to download and run code producing result, so naively i wrote in my org code :
#+begin_src sh
git clone [email protected]:xxx/yyy/mygitproject.git
#+end_src
Folder appear in ~/home/xxx/reproductible_paper/mygitproject/
as expected.
After that, i don't want to rewrite my :dir
path for each #+BEGIN_SRC ... #+END_SRC
block, so i use the :session capacity, and i wrote
#+begin_src sh
cd mygitproject
#+end_src
BUT, using this strategy cannot work.
When you write and export org file multiple time in a writing/publishing workflow (write -> execute/publish -> write), each command rerun using the same shell session !, bypassing the :header-args: dir:
defined in property.
The result was weird, and multiple run create an infinity of nested folder due to git clone and cd command.
Following your advice what is the best way to manage path in this use case ? Killing session each run ? Use environment variable without :session
to define directory path ?
UPDATE 1
a) Using absolute path, env substitution, and session :
:PROPERTIES:
:export_file_name: reproductiblepaper
:header-args: :dir ~/home/xxx/reproductible_paper tangle:yes session: reproductible
:END:
#+NAME: absfolder
#+BEGIN_SRC sh
ABS="/home/xxx/reproductible_paper"
#+END_SRC
#+begin_src sh
git clone [email protected]:xxx/yyy/mygitproject.git
#+end_src
#+begin_src sh :noweb yes
<<absfolder>>
cd $ABS/mygitproject
pwd
#+end_src
If you use this solution, and killing each time the session, it works, but you cannot use interactive evaluation without creating an unstable state in your shell session. Why not.
But to do this in a clean way i need :
- a way/command to kill/reset session at each re-export.
- a starting script which clean previous state files/folders manipulation
Any advice do that in org ?
b) I suppose there is probably another way to do the same thing without using a session, using :var or :dir and using link dependency between each code block ?
cd mproject/
: use absolute paths. For the git command, you could:[[ -d myproject ]] || git clone...
. But your issues now aren't about Org Mode or Org Babel anymore. These are just generic coding issues about how to write code that behaves properly if it is run multiple times. You'll have these same issues regardless of Org.