reddit / emacs

Org Mode as R data frames?

kammadeva · 2022-04-20 19:10
I feel like this is a no brainer but I find this difficult to research. Basically, I'd like to collect data in org mode format and use it in (potentially in-file) R code. ``` * Hydrogen ** Symbol H ** Atomic Number 1 ** Notes Foobar * Helium ** Symbol He ** Atomic Number 2 ** Notes Makes your voice funny ``` Is there any existing infrastructure to turn this into an R data frame that I just fail to find? Basically, I'd rather collect data in "plain" org mode (possibly across multiple files) than create formatted tables or dedicated code blocks.
Visual_Bumblebee_555 · 2022-04-21 03:13
This outline format is so close to R-language-supported YAML or even JSON. Are you sure you wouldn’t rather collect your data in the latter formats?
thevoidzero · 2022-04-21 03:39
Wow, never thought someone would need it, but since you're free to do it, it should be doable using any org parser, you just have to make the headings as keys for dictionaries (in python) and then once you have a json, you can load json in pandas (python library) which can give out csv. I just went and tried it: Org file contents: * hydrogen ** number 1 ** wt 1 * Helium ** number 2 ** neutrons 2 ** wt 4 Python script: import orgparse org = orgparse.load("/tmp/test.org") def org_to_dict(org_node): nodes = dict() if not org_node.children: return org_node.get_body() for n in org_node.children: nodes[n.get_heading()] = org_to_dict(n) return nodes org_to_dict(org) # will give you a dictionary import pandas as pd pd.DataFrame(org_to_dict(org)) # will give you a data frame, you can save it as csv or ... gives me: hydrogen Helium number 1 2 wt 1 4 neutrons NaN 2 Should be acceptable? You can modify it to however you want the final data to be parsed, or each heading to be parsed in the keys.
kammadeva · 2022-04-21 15:58
Hrm... looks like I've got to write an org parser in R then? I was hoping for a ready solution since to me it looks like something lots of people would want to use but maybe I'm wrong about that.
kammadeva · 2022-04-21 16:00
Yes, I'm thinking of stuff like character sheets, fact sheets and stuff that I can do simple statistics on. YAML and JSON are great formats for passing data from program to program or even for writing configuration to a certain degree, but they are not easily human readable when you get to a certain point.
thevoidzero · 2022-04-21 16:22
I don't think a lot of people would want this tbh. I personally would just write a csv. You can probably just write csv data row by row in org in src blocks and explain it and then tangle it at the end.
kammadeva · 2022-04-21 16:40
I'll reference my other comment: https://www.reddit.com/r/emacs/comments/u83zr8/org_mode_as_r_data_frames/i5mqo7e?utm_medium=android_app&utm_source=share&context=3 csv would force me to either have an unreadable format or to repeat myself (or run a conversion script which is ties back to the proposed solution)
globalcandyamnesia · 2022-04-22 04:14
I'm not an expert on R, so I don't know if this is sufficient, but [the 'ascii' package](https://github.com/mclements/ascii) for R claims to be able to parse org files. You can see all available parsers [here](https://orgmode.org/worg/org-tools/index.html)