emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Jeremie Juste <jeremiejuste@gmail.com>
To: Vikas Rawal <vikasrawal@gmail.com>
Cc: Naresh Gurbuxani <naresh_gurbuxani@hotmail.com>,
	"emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>
Subject: Re: Efficiently reuse code in org babel with R
Date: Thu, 07 Jul 2022 10:42:02 +0200	[thread overview]
Message-ID: <871quxiaat.fsf@gmail.com> (raw)
In-Reply-To: <CALtzAB1y+RNQ_urQc=c4+YG-otesjsZ=LjyBt=cA7PmP-BDemQ@mail.gmail.com> (Vikas Rawal's message of "Thu, 7 Jul 2022 09:45:00 +0530")

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	other threads:[~2022-07-07  8:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-07  3:31 Efficiently reuse code in org babel with R Naresh Gurbuxani
2022-07-07  4:15 ` Vikas Rawal
2022-07-07  8:42   ` Jeremie Juste [this message]
2022-07-07 12:09     ` Naresh Gurbuxani
2022-07-07  6:22 ` Greg Minshall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871quxiaat.fsf@gmail.com \
    --to=jeremiejuste@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=naresh_gurbuxani@hotmail.com \
    --cc=vikasrawal@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).