emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Efficiently reuse code in org babel with R


From: Jeremie Juste
Subject: Re: Efficiently reuse code in org babel with R
Date: Thu, 07 Jul 2022 10:42:02 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)

Hello Naresh,

Many thanks for sharing.
If I'm looking for efficiency, I tend not to use noweb and use an R
session instead.

One reason is that caching does not work with noweb

#+name: test
#+begin_src R  :cache yes :session *R*
Sys.sleep(10)
  a <- 1
#+end_src

#+RESULTS[36c41617bf9aa447ecc28fca8207eab38340d418]: test
: 1


#+name: add1
#+begin_src R :noweb yes :cache no :session *R*
<<test>>
 a + 1
#+end_src

#+RESULTS: add1
: 2


I would proceed in the following way

    #+name: connection
    #+begin_src R :exports results :results output :session *R* 
     ## my code here
    #+end_src

    #+begin_src R :exports results :results output graphics file
    :session *R* :file figures/fig1.png
    ## my code here
    #+end_src

    #+begin_src R :exports results output graphics file :file figures/fig2.png 
:session *R*
    ## my code here
    #+end_src

this will require running the first code chunk once only to run the rest

On Thursday,  7 Jul 2022 at 09:22, Greg Minshall wrote:
>  you'd have to be careful to not get bitten by the code
> *not* being evaluated, and by it being evaluated too often.  (probably
> too clever by half.)

The comment of Greg is very relevant here as you'll have to manage
updates yourself, and it can get complex very quickly. A potential
pitfall is when the tables in database you are referring to, change.
So in this case I would avoid using :cache yes in the first code chunk


I'm also tempted to adopt the following organization

    * setup table
    #+name: connection
    #+begin_src R :exports results :results output :session *R* 
     ## my code here
    #+end_src
    
    * output graphs
     :PROPERTIES:
     :header-args:R: :exports results output graphics file  :session *R*
     :END:
     
    #+begin_src R  :file figures/fig2.png
    ## my code here
    #+end_src

    #+begin_src R  :file figures/fig1.png 
    ## my code here
    #+end_src

then I would use (org-babel-execute-subtree) which is bound to C-c C-v
s on the subheading output graphs to regenerate the graphs.


HTH,
Jeremie



reply via email to

[Prev in Thread] Current Thread [Next in Thread]