emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How to pass named table reference in source block variable
@ 2013-08-06 21:31 Roland Donat
  2013-08-06 22:22 ` Thorsten Jolitz
  0 siblings, 1 reply; 10+ messages in thread
From: Roland Donat @ 2013-08-06 21:31 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

I have the following table :
#+TBLNAME: T
|   | x | 1     |
| ^ |   | varx  |

And I would like to use the reference T$var_x (=1) as input in a source block 
variable. 
For example, I would have expected the following behavior for this source 
code :
#+begin_src python :var x=T$varx  :return x
x
#+end_src

#+RESULTS:
: 1

But instead, I get the emacs message : org-babel-ref-resolve: Reference 
'T$varx' not found in this
buffer

Any idea to produce the desired result would be much appreciated!

Thanks you in advance.

Roland.
 

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

* Re: How to pass named table reference in source block variable
  2013-08-06 21:31 How to pass named table reference in source block variable Roland Donat
@ 2013-08-06 22:22 ` Thorsten Jolitz
  2013-08-06 22:27   ` Nick Dokos
  2013-08-07  8:28   ` Roland Donat
  0 siblings, 2 replies; 10+ messages in thread
From: Thorsten Jolitz @ 2013-08-06 22:22 UTC (permalink / raw)
  To: emacs-orgmode

Roland Donat <roland.donat@gmail.com> writes:

> Hello,
>
> I have the following table :
> #+TBLNAME: T
> |   | x | 1     |
> | ^ |   | varx  |
>
> And I would like to use the reference T$var_x (=1) as input in a source block 
> variable. 
> For example, I would have expected the following behavior for this source 
> code :
> #+begin_src python :var x=T$varx  :return x
> x
> #+end_src
>
> #+RESULTS:
> : 1
>
> But instead, I get the emacs message : org-babel-ref-resolve: Reference 
> 'T$varx' not found in this
> buffer
>
> Any idea to produce the desired result would be much appreciated!
>
> Thanks you in advance.
>
> Roland.

This does the job in Emacs Lisp:

 #+TBLNAME: T
 |   | x | 1     |
 | ^ |   | varx  |

#+begin_src emacs-lisp :var x=T[0,-1]
 x
#+end_src

#+results:
: 1

-- 
cheers,
Thorsten

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

* Re: How to pass named table reference in source block variable
  2013-08-06 22:22 ` Thorsten Jolitz
@ 2013-08-06 22:27   ` Nick Dokos
  2013-08-07  8:28   ` Roland Donat
  1 sibling, 0 replies; 10+ messages in thread
From: Nick Dokos @ 2013-08-06 22:27 UTC (permalink / raw)
  To: emacs-orgmode

Thorsten Jolitz <tjolitz@gmail.com> writes:

> Roland Donat <roland.donat@gmail.com> writes:
>
>> Hello,
>>
>> I have the following table :
>> #+TBLNAME: T
>
> This does the job in Emacs Lisp:
>
>  #+TBLNAME: T
>  |   | x | 1     |
>  | ^ |   | varx  |
>
> #+begin_src emacs-lisp :var x=T[0,-1]
>  x
> #+end_src
>
> #+results:
> : 1

Nit: I believe #+TBLNAME is obsolete/deprecated/frowned upon - use
#+name going forward.

-- 
Nick

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

* Re: How to pass named table reference in source block variable
  2013-08-06 22:22 ` Thorsten Jolitz
  2013-08-06 22:27   ` Nick Dokos
@ 2013-08-07  8:28   ` Roland Donat
  2013-08-07 15:29     ` Thomas S. Dye
  1 sibling, 1 reply; 10+ messages in thread
From: Roland Donat @ 2013-08-07  8:28 UTC (permalink / raw)
  To: emacs-orgmode

Thorsten Jolitz <tjolitz <at> gmail.com> writes:

> 
> This does the job in Emacs Lisp:
> 
>  #+TBLNAME: T
>  |   | x | 1     |
>  | ^ |   | varx  |
> 
> #+begin_src emacs-lisp :var x=T[0,-1]
>  x
> #+end_src
> 
> #+results:
> : 1
> 

Thanks for the answer but in fact, my objective is precisely to avoid using 
the indices of the value I want to pass as input of the code block.

My goal is to use the cell name reference "varx" which would make the code 
block simpler to maintain. Indeed, if I add new data on the top of table T, 
I wouldn't have to change the reference in the code block since the name 
reference is fixed.

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

* Re: How to pass named table reference in source block variable
  2013-08-07  8:28   ` Roland Donat
@ 2013-08-07 15:29     ` Thomas S. Dye
  2013-08-07 20:06       ` Roland Donat
  0 siblings, 1 reply; 10+ messages in thread
From: Thomas S. Dye @ 2013-08-07 15:29 UTC (permalink / raw)
  To: Roland Donat; +Cc: emacs-orgmode

Roland Donat <roland.donat@gmail.com> writes:

> Thorsten Jolitz <tjolitz <at> gmail.com> writes:
>
>> 
>> This does the job in Emacs Lisp:
>> 
>>  #+TBLNAME: T
>>  |   | x | 1     |
>>  | ^ |   | varx  |
>> 
>> #+begin_src emacs-lisp :var x=T[0,-1]
>>  x
>> #+end_src
>> 
>> #+results:
>> : 1
>> 
>
> Thanks for the answer but in fact, my objective is precisely to avoid using 
> the indices of the value I want to pass as input of the code block.
>
> My goal is to use the cell name reference "varx" which would make the code 
> block simpler to maintain. Indeed, if I add new data on the top of table T, 
> I wouldn't have to change the reference in the code block since the name 
> reference is fixed.
>
>

Perhaps this can help:

http://orgmode.org/worg/org-contrib/babel/examples/lob-table-operations.html

Alternatively, you might pass the table to a code block of a language
that understands tables, such as an R data frame, and use that language
to retrieve values by name.

hth,
Tom

-- 
Thomas S. Dye
http://www.tsdye.com

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

* Re: How to pass named table reference in source block variable
  2013-08-07 15:29     ` Thomas S. Dye
@ 2013-08-07 20:06       ` Roland Donat
  2013-08-07 21:53         ` Thomas S. Dye
  0 siblings, 1 reply; 10+ messages in thread
From: Roland Donat @ 2013-08-07 20:06 UTC (permalink / raw)
  To: emacs-orgmode

Thomas S. Dye <tsd <at> tsdye.com> writes:


> 
> Perhaps this can help:
> 
> http://orgmode.org/worg/org-contrib/babel/examples/lob-table-
operations.html
> 
> Alternatively, you might pass the table to a code block of a language
> that understands tables, such as an R data frame, and use that language
> to retrieve values by name.
> 
> hth,
> Tom
> 

Thank you for the link, I'll check it but seems that it won't solve the 
problem. But anyway, I found a workaround that doesn't involve to insert 
table reference. 

It's a pity that I am so bad at Lisp because I feel this feature wouldn't be 
too complicated to code, especially if the reference mechanism is already 
implemented.

Thank you again!

Cheers,

Roland.
 

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

* Re: How to pass named table reference in source block variable
  2013-08-07 20:06       ` Roland Donat
@ 2013-08-07 21:53         ` Thomas S. Dye
  2013-08-08  7:55           ` Roland Donat
  0 siblings, 1 reply; 10+ messages in thread
From: Thomas S. Dye @ 2013-08-07 21:53 UTC (permalink / raw)
  To: Roland Donat; +Cc: emacs-orgmode

Roland Donat <roland.donat@gmail.com> writes:

>> 
>> Perhaps this can help:
>> 
>> http://orgmode.org/worg/org-contrib/babel/examples/lob-table-
> operations.html
>> 
>> Alternatively, you might pass the table to a code block of a language
>> that understands tables, such as an R data frame, and use that language
>> to retrieve values by name.
>> 
>> hth,
>> Tom
>> 
>
> Thank you for the link, I'll check it but seems that it won't solve the 
> problem. But anyway, I found a workaround that doesn't involve to insert 
> table reference. 

What is the workaround?

All the best,
Tom
-- 
Thomas S. Dye
http://www.tsdye.com

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

* Re: How to pass named table reference in source block variable
  2013-08-07 21:53         ` Thomas S. Dye
@ 2013-08-08  7:55           ` Roland Donat
  2013-08-08 15:10             ` Eric Schulte
  0 siblings, 1 reply; 10+ messages in thread
From: Roland Donat @ 2013-08-08  7:55 UTC (permalink / raw)
  To: emacs-orgmode

Thomas S. Dye <tsd <at> tsdye.com> writes:

> 
> Roland Donat <roland.donat <at> gmail.com> writes:
> 
> >> 
> >> Perhaps this can help:
> >> 
> >> http://orgmode.org/worg/org-contrib/babel/examples/lob-table-
> > operations.html
> >> 
> >> Alternatively, you might pass the table to a code block of a language
> >> that understands tables, such as an R data frame, and use that language
> >> to retrieve values by name.
> >> 
> >> hth,
> >> Tom
> >> 
> >
> > Thank you for the link, I'll check it but seems that it won't solve the 
> > problem. But anyway, I found a workaround that doesn't involve to insert 
> > table reference. 
> 
> What is the workaround?
> 
> All the best,
> Tom

Well, my main objective was to write piece of code in a given language X 
including information stored in some org-table. 

So my solution for now was to create some python functions able to generate 
my code. I use babel to pass my org-table as input to the python function 
and the result is another source block of language X containing the code 
taking into account the information of my org-table.

I recognized that the solution seems quite heavy but I have already 
developped some python wrapper for my language X so It takes me only 2 hours 
to do the job.

All the best.

Roland. 

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

* Re: How to pass named table reference in source block variable
  2013-08-08  7:55           ` Roland Donat
@ 2013-08-08 15:10             ` Eric Schulte
  2013-08-08 15:42               ` Roland Donat
  0 siblings, 1 reply; 10+ messages in thread
From: Eric Schulte @ 2013-08-08 15:10 UTC (permalink / raw)
  To: Roland Donat; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1652 bytes --]

Roland Donat <roland.donat@gmail.com> writes:

> Thomas S. Dye <tsd <at> tsdye.com> writes:
>
>> 
>> Roland Donat <roland.donat <at> gmail.com> writes:
>> 
>> >> 
>> >> Perhaps this can help:
>> >> 
>> >> http://orgmode.org/worg/org-contrib/babel/examples/lob-table-
>> > operations.html
>> >> 
>> >> Alternatively, you might pass the table to a code block of a language
>> >> that understands tables, such as an R data frame, and use that language
>> >> to retrieve values by name.
>> >> 
>> >> hth,
>> >> Tom
>> >> 
>> >
>> > Thank you for the link, I'll check it but seems that it won't solve the 
>> > problem. But anyway, I found a workaround that doesn't involve to insert 
>> > table reference. 
>> 
>> What is the workaround?
>> 
>> All the best,
>> Tom
>
> Well, my main objective was to write piece of code in a given language X 
> including information stored in some org-table. 
>
> So my solution for now was to create some python functions able to generate 
> my code. I use babel to pass my org-table as input to the python function 
> and the result is another source block of language X containing the code 
> taking into account the information of my org-table.
>
> I recognized that the solution seems quite heavy but I have already 
> developped some python wrapper for my language X so It takes me only 2 hours 
> to do the job.
>
> All the best.
>

It sounds like you want to use tables like key-value stores.  I think
adding such behavior directly to Org-mode would overly complicate the
data structures passed between code blocks (which currently only
consists of scalars and tables).  However, maybe the following could
work.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: key-value.org --]
[-- Type: text/x-org, Size: 776 bytes --]

Here's an example table with key/value data.
#+name: table
| keys | values |
|------+--------|
| foo  |      1 |
| bar  |      2 |
| baz  |      3 |
| qux  |      4 |

Here's a code block which can access the data in such a table by key.
This could be added to the library of babel to make it usable from any
Org-mode file.
#+name: by-key
#+begin_src sh :var data="" :var key=""
  echo "$data"|awk "{if(\$1 == \"$key\") print \$2}"
#+end_src

And here's an example usage.
#+headers: :results verbatim
#+begin_src sh :var foo=by-key(table,"foo") :var baz=by-key(table,"baz")
  cat <<EOF
  Pulling the data from the above table we find
  that foo is $foo and baz is $baz.
  EOF
#+end_src

#+RESULTS:
: Pulling the data from the above table we find
: that foo is 1 and baz is 3.

[-- Attachment #3: Type: text/plain, Size: 71 bytes --]


Cheers,

-- 
Eric Schulte
http://cs.unm.edu/~eschulte
PGP: 0x614CA05D

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

* Re: How to pass named table reference in source block variable
  2013-08-08 15:10             ` Eric Schulte
@ 2013-08-08 15:42               ` Roland Donat
  0 siblings, 0 replies; 10+ messages in thread
From: Roland Donat @ 2013-08-08 15:42 UTC (permalink / raw)
  To: emacs-orgmode

Eric Schulte <schulte.eric <at> gmail.com> writes:

> 
> It sounds like you want to use tables like key-value stores.  I think
> adding such behavior directly to Org-mode would overly complicate the
> data structures passed between code blocks (which currently only
> consists of scalars and tables).  However, maybe the following could
> work.
> 
> 
> Attachment (key-value.org): text/x-org, 776 bytes
> 
> 
> Cheers,
> 

Thanks for the attachment. It works fine indeed!
But should it be so complicated to add this behavior to Org-mode?

I can rewrite your table as follows :

#+name: table
|   | keys | values |
|---+------+--------|
|   | foo  | 1      |
| ^ |      | foo    |
|   | bar  | 2      |
| ^ |      | bar    |

Now I can refer to bar value as $bar in org-table. 

So, my suggestion is only to mimic this feature to assign value of source 
block variable like :

#+headers: :results verbatim
#+begin_src sh :var foo=table$foo :var bar=table$bar
  cat <<EOF
  Pulling the data from the above table we find
  that foo is $foo and baz is $bar.
  EOF
#+end_src

But anyway, thanks for the solution.

Cheers.

Roland.

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

end of thread, other threads:[~2013-08-08 15:42 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-06 21:31 How to pass named table reference in source block variable Roland Donat
2013-08-06 22:22 ` Thorsten Jolitz
2013-08-06 22:27   ` Nick Dokos
2013-08-07  8:28   ` Roland Donat
2013-08-07 15:29     ` Thomas S. Dye
2013-08-07 20:06       ` Roland Donat
2013-08-07 21:53         ` Thomas S. Dye
2013-08-08  7:55           ` Roland Donat
2013-08-08 15:10             ` Eric Schulte
2013-08-08 15:42               ` Roland Donat

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