emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [babel] noweb reference with default values
@ 2015-12-14 13:28 Andreas Leha
  2015-12-14 16:45 ` Charles C. Berry
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Leha @ 2015-12-14 13:28 UTC (permalink / raw)
  To: emacs-orgmode

Hi all,

I'd like to hear your ideas on how to include noweb references to code
blocks in a way that the default values are used as parameter values.

Here is a little example:

#+PROPERTY: header-args:R :session *testR*

The background is that I like to use Org mode table to record small data.

#+name: datatable
| A |  B |
|---+----|
| 1 | 10 |
| 2 | 20 |

Usually I want to pre-process and/or convert such data.

#+name: read_datatable
#+header: :var datatable=datatable
#+begin_src R :results none
  datatable$B <- 10 * datatable$B 
#+end_src

Later I would like to use that data in larger (R-) code blocks.  I'd
like such code blocks to DWIM:

#+name: some_code
#+begin_src R :noweb yes :results graphics :file testplot.png
  <<read_datatable>>
  plot(datatable$A, datatable$B)
#+end_src

But they do not: They are not stand alone and do not execute -- unless I
executed `read_datatable' manually/by chance upfront.



My current solution is to export the table to csv and read that csv in
`read_datatable'.

If the result of `read_datatable' is a single table again (as in this
MWE) I usually return that to Org mode which works ok.  But often the
result is more complex than that.

I see two ways to go:
- I could serialize more complex objects to disk and return a file name.
  But I'd prefer to not go through disk.
- I could pass the same arguments as to `read_datatable' also to
  `some_code'.  But that is duplication and becomes quite convoluted in
  the case of several noweb references with several arguments each.

Any ideas how to do that better?

Thanks,
Andreas

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [babel] noweb reference with default values
  2015-12-14 13:28 [babel] noweb reference with default values Andreas Leha
@ 2015-12-14 16:45 ` Charles C. Berry
  2015-12-14 22:18   ` Andreas Leha
  0 siblings, 1 reply; 4+ messages in thread
From: Charles C. Berry @ 2015-12-14 16:45 UTC (permalink / raw)
  To: Andreas Leha; +Cc: emacs-orgmode

On Mon, 14 Dec 2015, Andreas Leha wrote:

> Hi all,
>
> I'd like to hear your ideas on how to include noweb references to code
> blocks in a way that the default values are used as parameter values.
>
> Here is a little example:
>
> #+PROPERTY: header-args:R :session *testR*
>
> The background is that I like to use Org mode table to record small data.
>
> #+name: datatable
> | A |  B |
> |---+----|
> | 1 | 10 |
> | 2 | 20 |
>
> Usually I want to pre-process and/or convert such data.
>
> #+name: read_datatable
> #+header: :var datatable=datatable
> #+begin_src R :results none
>  datatable$B <- 10 * datatable$B
> #+end_src
>
> Later I would like to use that data in larger (R-) code blocks.  I'd
> like such code blocks to DWIM:
>
> #+name: some_code
> #+begin_src R :noweb yes :results graphics :file testplot.png
>  <<read_datatable>>
>  plot(datatable$A, datatable$B)
> #+end_src
>
> But they do not: They are not stand alone and do not execute -- unless I
> executed `read_datatable' manually/by chance upfront.

Try this:

#+name: read_datatable
#+header: :var datatable=datatable
#+begin_src R :results value :colnames yes
   datatable$B <- 10 * datatable$B
   datatable
#+end_src

#+name: some_code
#+HEADER: :var datatable=read_datatable()
#+begin_src R :noweb yes :results graphics :file testplot.png
plot(datatable$A, datatable$B)
#+end_src


HTH,

Chuck

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [babel] noweb reference with default values
  2015-12-14 16:45 ` Charles C. Berry
@ 2015-12-14 22:18   ` Andreas Leha
  2015-12-17 11:47     ` Andreas Leha
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Leha @ 2015-12-14 22:18 UTC (permalink / raw)
  To: emacs-orgmode

Hi Chuck,

"Charles C. Berry" <ccberry@ucsd.edu> writes:
> On Mon, 14 Dec 2015, Andreas Leha wrote:
>
>> Hi all,
>>
>> I'd like to hear your ideas on how to include noweb references to code
>> blocks in a way that the default values are used as parameter values.
>>
>> Here is a little example:
>>
>> #+PROPERTY: header-args:R :session *testR*
>>
>> The background is that I like to use Org mode table to record small data.
>>
>> #+name: datatable
>> | A |  B |
>> |---+----|
>> | 1 | 10 |
>> | 2 | 20 |
>>
>> Usually I want to pre-process and/or convert such data.
>>
>> #+name: read_datatable
>> #+header: :var datatable=datatable
>> #+begin_src R :results none
>>  datatable$B <- 10 * datatable$B
>> #+end_src
>>
>> Later I would like to use that data in larger (R-) code blocks.  I'd
>> like such code blocks to DWIM:
>>
>> #+name: some_code
>> #+begin_src R :noweb yes :results graphics :file testplot.png
>>  <<read_datatable>>
>>  plot(datatable$A, datatable$B)
>> #+end_src
>>
>> But they do not: They are not stand alone and do not execute -- unless I
>> executed `read_datatable' manually/by chance upfront.
>
> Try this:
>
> #+name: read_datatable
> #+header: :var datatable=datatable
>
> #+begin_src R :results value :colnames yes
>    datatable$B <- 10 * datatable$B
>    datatable
> #+end_src
>
> #+name: some_code
> #+HEADER: :var datatable=read_datatable()
>
> #+begin_src R :noweb yes :results graphics :file testplot.png
> plot(datatable$A, datatable$B)
> #+end_src
>

Thanks!  I am aware of that possibility.  Should have posted a more
involved example.  This works if I only return a table.  Or something
else, that can be passed through Org.  But it fails for instance if the
result is a function (or more functions...).

Regards,
Andreas

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [babel] noweb reference with default values
  2015-12-14 22:18   ` Andreas Leha
@ 2015-12-17 11:47     ` Andreas Leha
  0 siblings, 0 replies; 4+ messages in thread
From: Andreas Leha @ 2015-12-17 11:47 UTC (permalink / raw)
  To: emacs-orgmode

Hi all,

Andreas Leha <andreas.leha@med.uni-goettingen.de> writes:
> Hi Chuck,
>
> "Charles C. Berry" <ccberry@ucsd.edu> writes:
>> On Mon, 14 Dec 2015, Andreas Leha wrote:
>>
>>> Hi all,
>>>
>>> I'd like to hear your ideas on how to include noweb references to code
>>> blocks in a way that the default values are used as parameter values.
>>>
>>> Here is a little example:
>>>
>>> #+PROPERTY: header-args:R :session *testR*
>>>
>>> The background is that I like to use Org mode table to record small data.
>>>
>>> #+name: datatable
>>> | A |  B |
>>> |---+----|
>>> | 1 | 10 |
>>> | 2 | 20 |
>>>
>>> Usually I want to pre-process and/or convert such data.
>>>
>>> #+name: read_datatable
>>> #+header: :var datatable=datatable
>>> #+begin_src R :results none
>>>  datatable$B <- 10 * datatable$B
>>> #+end_src
>>>
>>> Later I would like to use that data in larger (R-) code blocks.  I'd
>>> like such code blocks to DWIM:
>>>
>>> #+name: some_code
>>> #+begin_src R :noweb yes :results graphics :file testplot.png
>>>  <<read_datatable>>
>>>  plot(datatable$A, datatable$B)
>>> #+end_src
>>>
>>> But they do not: They are not stand alone and do not execute -- unless I
>>> executed `read_datatable' manually/by chance upfront.
>>
>> Try this:
>>
>> #+name: read_datatable
>> #+header: :var datatable=datatable
>>
>> #+begin_src R :results value :colnames yes
>>    datatable$B <- 10 * datatable$B
>>    datatable
>> #+end_src
>>
>> #+name: some_code
>> #+HEADER: :var datatable=read_datatable()
>>
>> #+begin_src R :noweb yes :results graphics :file testplot.png
>> plot(datatable$A, datatable$B)
>> #+end_src
>>
>
> Thanks!  I am aware of that possibility.  Should have posted a more
> involved example.  This works if I only return a table.  Or something
> else, that can be passed through Org.  But it fails for instance if the
> result is a function (or more functions...).
>

In case anyone is interested.  I came up with a quite simple code block
that does what I want:

--8<---------------cut here---------------start------------->8---
#+name: org_exec
#+header: :var srcblock=""
#+begin_src emacs-lisp :results silent
  (save-excursion
    (org-babel-goto-named-src-block srcblock)
    (org-babel-execute-src-block)
  )
  (concat srcblock " executed")
#+end_src
--8<---------------cut here---------------end--------------->8---

Now, I can do

--8<---------------cut here---------------start------------->8---
#+name: some_code
#+begin_src R :session *testR* :noweb yes :results graphics :file testplot.png
  <<org_exec("read_datatable")>>
  plot(datatable$A, datatable$B)
#+end_src
--8<---------------cut here---------------end--------------->8---

The remaining downside is that I have to take care of running things in
the correct session.

Best,
Andreas

PS: complete example:
--8<---------------cut here---------------start------------->8---
* Org

#+name: org_exec
#+header: :var srcblock=""
#+begin_src emacs-lisp :results silent
  (save-excursion
    (org-babel-goto-named-src-block srcblock)
    (org-babel-execute-src-block)
  )
  (concat "\"" srcblock " executed" "\"")
#+end_src


* Data

#+name: datatable
| A |  B |
|---+----|
| 1 | 10 |
| 2 | 20 |


#+name: read_datatable
#+header: :var datatable=datatable
#+begin_src R :session *testR* :results value :colnames yes
   datatable$B <- 10 * datatable$B
   datatable
#+end_src

#+results: read_datatable
| A |   B |
|---+-----|
| 1 | 100 |
| 2 | 200 |

* Analysis

#+name: some_code
#+begin_src R :session *testR* :noweb yes :results graphics :file testplot.png
  <<org_exec("read_datatable")>>
  plot(datatable$A, datatable$B)
#+end_src

#+results: some_code
[[file:testplot.png]]

--8<---------------cut here---------------end--------------->8---

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-12-17 11:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-12-14 13:28 [babel] noweb reference with default values Andreas Leha
2015-12-14 16:45 ` Charles C. Berry
2015-12-14 22:18   ` Andreas Leha
2015-12-17 11:47     ` Andreas Leha

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).