all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Org babel Python and R, table not transformed when :var is called ?
@ 2021-12-01  8:04 Sébastien Rey-Coyrehourcq
  2021-12-02 11:10 ` Sébastien Rey-Coyrehourcq
  2022-10-16 11:23 ` Ihor Radchenko
  0 siblings, 2 replies; 3+ messages in thread
From: Sébastien Rey-Coyrehourcq @ 2021-12-01  8:04 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1.1.1: Type: text/plain, Size: 1880 bytes --]

Hi there,

Perhaps it's a bug, or something i don't understand, i post on reddit 
(https://www.reddit.com/r/emacs/comments/r5yt4a/r_talking_with_python_using_orgtable_not_work/) 
to see if people on org community know the problem.

I cross post here to see if it's a know bug or somethings ?

There is something i don't understand,
a difference between behavior of org-babel and org-mode when you use org 
table to pass data using or not using :var.

|#+NAME:mypythoncode #+begin_src python :results value raw :output 
:return tabulate(df, headers=df.columns, tablefmt='orgtbl') import numpy 
as np import pandas as pd from tabulate import tabulate df = 
pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD')) 
#+end_src #+RESULTS: mypythoncode | | A | B | C | D | 
|---+---+---+---+---| | 0 | 0 | 9 | 6 | 0 | | 1 | 2 | 9 | 0 | 4 | | 2 | 
9 | 6 | 0 | 1 | | 3 | 6 | 1 | 8 | 1 | | 4 | 4 | 2 | 1 | 4 | | 5 | 2 | 1 
| 1 | 1 | | 6 | 4 | 8 | 9 | 0 | | 7 | 1 | 4 | 8 | 7 | | 8 | 9 | 3 | 2 | 
5 | | 9 | 5 | 0 | 7 | 3 | #+NAME:lib-R #+HEADER: :var code=mypythoncode 
#+begin_src R :results output library(ggplot2) library(dplyr) 
library(lubridate) str(code) #+end_src #+RESULTS: lib-R : chr "| | A | B 
| C | D |\n|----+-----+-----+-----+-----|\n| 0 | 8 | 0 | 5 | 2 |\n| 1 | 
2 | "| __truncated__ |

As you see, the org table is not recognized as a dataframe by R.

If i replace by a basic org table :

|||#+NAME: any_data | | parameter | value | |---+-----------+-------| | 0 
| heats | 30 | | 1 | heats | 30 | #+NAME:lib-R #+HEADER: :var 
code=any_data #+begin_src R :results output library(ggplot2) 
library(dplyr) library(lubridate) str(code) #+end_src #+RESULTS: lib-R : 
'data.frame': 2 obs. of 3 variables: : $ X : int 0 1 : $ parameter: chr 
"heats" "heats" : $ value : int 30 30 |||

That works...

Best regards,

||


[-- Attachment #1.1.1.2: Type: text/html, Size: 2892 bytes --]

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 8819 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: Org babel Python and R, table not transformed when :var is called ?
  2021-12-01  8:04 Org babel Python and R, table not transformed when :var is called ? Sébastien Rey-Coyrehourcq
@ 2021-12-02 11:10 ` Sébastien Rey-Coyrehourcq
  2022-10-16 11:23 ` Ihor Radchenko
  1 sibling, 0 replies; 3+ messages in thread
From: Sébastien Rey-Coyrehourcq @ 2021-12-02 11:10 UTC (permalink / raw)
  To: emacs-orgmode, jeremiejuste

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

Hi,

I found a solution using *Wrapping*, adding a #+NAME to #+RESULTS block 
resolve the problem.

This is not defined / described into the doc, and actually i found this 
process a little anti-intuitive. #+RESULTS has already a name, 
resulting/linked from/to the execution of mypythoncode ...

Best regards


#+NAME: attr_wrap
#+BEGIN_SRC sh :var data="" :results output
   echo "#+NAME: cities"
   echo "$data"
#+END_SRC

#+NAME:mypythoncode
#+begin_src python  :results value raw :output  :post 
attr_wrap(data=*this*) :return tabulate(df, headers=df.columns, 
tablefmt='orgtbl')
import numpy as np
import pandas as pd
from tabulate import tabulate
df = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), 
columns=list('ABCD'))
#+end_src

Le 01/12/2021 à 09:04, Sébastien Rey-Coyrehourcq a écrit :
>
> Hi there,
>
> Perhaps it's a bug, or something i don't understand, i post on reddit 
> (https://www.reddit.com/r/emacs/comments/r5yt4a/r_talking_with_python_using_orgtable_not_work/) 
> to see if people on org community know the problem.
>
> I cross post here to see if it's a know bug or somethings ?
>
> There is something i don't understand,
> a difference between behavior of org-babel and org-mode when you use 
> org table to pass data using or not using :var.
>
> |#+NAME:mypythoncode #+begin_src python :results value raw :output 
> :return tabulate(df, headers=df.columns, tablefmt='orgtbl') import 
> numpy as np import pandas as pd from tabulate import tabulate df = 
> pd.DataFrame(np.random.randint(0,10,size=(10, 4)), 
> columns=list('ABCD')) #+end_src #+RESULTS: mypythoncode | | A | B | C 
> | D | |---+---+---+---+---| | 0 | 0 | 9 | 6 | 0 | | 1 | 2 | 9 | 0 | 4 
> | | 2 | 9 | 6 | 0 | 1 | | 3 | 6 | 1 | 8 | 1 | | 4 | 4 | 2 | 1 | 4 | | 
> 5 | 2 | 1 | 1 | 1 | | 6 | 4 | 8 | 9 | 0 | | 7 | 1 | 4 | 8 | 7 | | 8 | 
> 9 | 3 | 2 | 5 | | 9 | 5 | 0 | 7 | 3 | #+NAME:lib-R #+HEADER: :var 
> code=mypythoncode #+begin_src R :results output library(ggplot2) 
> library(dplyr) library(lubridate) str(code) #+end_src #+RESULTS: lib-R 
> : chr "| | A | B | C | D |\n|----+-----+-----+-----+-----|\n| 0 | 8 | 
> 0 | 5 | 2 |\n| 1 | 2 | "| __truncated__ |
>
> As you see, the org table is not recognized as a dataframe by R.
>
> If i replace by a basic org table :
>
> |||#+NAME: any_data | | parameter | value | |---+-----------+-------| | 
> 0 | heats | 30 | | 1 | heats | 30 | #+NAME:lib-R #+HEADER: :var 
> code=any_data #+begin_src R :results output library(ggplot2) 
> library(dplyr) library(lubridate) str(code) #+end_src #+RESULTS: lib-R 
> : 'data.frame': 2 obs. of 3 variables: : $ X : int 0 1 : $ parameter: 
> chr "heats" "heats" : $ value : int 30 30 |||
>
> That works...
>
> Best regards,
>
> ||

[-- Attachment #2: Type: text/html, Size: 4193 bytes --]

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

* Re: Org babel Python and R, table not transformed when :var is called ?
  2021-12-01  8:04 Org babel Python and R, table not transformed when :var is called ? Sébastien Rey-Coyrehourcq
  2021-12-02 11:10 ` Sébastien Rey-Coyrehourcq
@ 2022-10-16 11:23 ` Ihor Radchenko
  1 sibling, 0 replies; 3+ messages in thread
From: Ihor Radchenko @ 2022-10-16 11:23 UTC (permalink / raw)
  To: sebastien.rey-coyrehourcq; +Cc: emacs-orgmode

Sébastien Rey-Coyrehourcq <sebastien.rey-coyrehourcq@univ-rouen.fr>
writes:

> Perhaps it's a bug, or something i don't understand, i post on reddit 
> (https://www.reddit.com/r/emacs/comments/r5yt4a/r_talking_with_python_using_orgtable_not_work/) 
> to see if people on org community know the problem.
>
> I cross post here to see if it's a know bug or somethings ?
>
> There is something i don't understand,
> a difference between behavior of org-babel and org-mode when you use org 
> table to pass data using or not using :var.
>
> |#+NAME:mypythoncode #+begin_src python :results value raw :output 
> :return tabulate(df, headers=df.columns, tablefmt='orgtbl')

AFAIU, your tabulate call returns string, not a python table. So, :var
assignment indeed assigns a string. The returned string also happens to
use Org table format, which is why results block is an Org table.

By putting #+NAME to results block, you will be referring to its Org
syntax, which is then converted from Org table to R table, in contrast
to from Python result string to R string.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

end of thread, other threads:[~2022-10-16 11:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-12-01  8:04 Org babel Python and R, table not transformed when :var is called ? Sébastien Rey-Coyrehourcq
2021-12-02 11:10 ` Sébastien Rey-Coyrehourcq
2022-10-16 11:23 ` Ihor Radchenko

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.