Multiple org-mode tables sharing one #+TBLFM line - emacs

I have a structured org document with tables, one for each month. Each table does the same calculations, so, the #+TBLFM line is the same in all tables.
Is there a way to force all tables share the same #+TBLFM line?
Thanks in advance

You can share formulas if they're defined with emacs lisp. Define an emacs lisp function which handles the calculations, then reference that emacs lisp function in each of the tables. This is done in #+TBLFM using a quote ' before the lisp expression.
For example,
#+NAME: table1
| Col1 | Col2 |
|------+---------------|
| | do stuff here |
| | |
|------+---------------|
#+TBLFM: #2$2='(my-shared-function)
#+NAME: table2
| Col1 | Col2 |
|---------------+------|
| | |
| do stuff here | |
|---------------+------|
#+TBLFM: #3$1='(my-shared-function)
#+begin_src emacs-lisp
(defun my-shared-function ()
"Function to be shared between tables."
(interactive)
"do stuff here")
#+end_src
In the example, there is an emacs lisp function defined called my-shared-function which returns the string "do stuff here" (by virtue of being the return value of last expression evaluated). Each of the table calls the function, resulting in the string being place in the indicated cell (remember that # is row and $ is column).
You can read more about using emacs lisp as formulas in the manual:
https://orgmode.org/worg/org-tutorials/org-spreadsheet-lisp-formulas.html

Related

Named column formulas in an org mode table

I have the following emacs org mode table.
|---+-----+-----+-----+-----|
| ! | foo | bar | baz | duu |
| # | -5 | 2 | | |
To calculate the values of baz and duu I have the formulas
#+TBLFM: $4=vsum($foo..$bar)::$duu=vsum($foo..$bar)
When I reevaluate all formulas with C-u C-c C-c or C-u C-c *, the value of baz is computed fine, but the value of duu remains empty. Since the only difference between the formulas for baz and duu is that the former uses a number reference and the latter a named reference, I assume that one cannot use named column reference on the left side of the assignment operator. However I find it rather inconvenient as I don't want to hardcode the column numbers because I might need to add/remove columns in the future.
Is there a way to create a column formula that uses names for all columns involved?

How to execute inline source blocks in an org-mode table?

I would like to have the output of src_R blocks in an org-mode table:
| Variable | Value |
|----------+----------|
| x | src_R{x} |
However, when I export to PDF (via LaTeX) I get the literal src_R{x} rather than the value of the x variable in the underlying R session. I can use the same src_R{x} in text and it works as expected.
Is there a way to support inline source code in tables?
(I have seen this question with a similar title: Code blocks inside tables for org-mode, but the topic is different.)
Thanks to the prompt by Juancho (see comments), I have found the answer here: http://orgmode.org/worg/org-contrib/babel/intro.html#spreadsheet. I first define a named source block to perform my R computation:
#+NAME: my-code
#+BEGIN_SRC R :results output
message(10)
#+END_SRC
(Imagine the output is simply the number 10). Then I insert it into the table like this:
| Variable | Value |
|----------+-------|
| Name | |
#+TBLFM: #2$2='(org-sbe my-code)
Comments:
It seems that org-babel-execute is no longer there, the docs use org-sbe, which works with my 9.0.x org-mode version.
I have wrapped the code output in message() to avoid extra output from R. I have tried various header arguments to the R code (e.g., :results value raw) but I get either extra parentheses, presumably from lisp, or errors.
With org-sbe you can also pass arguments to the code, and even the output of other code blocks. This is explained in the docs referenced above.

Org Babel can't execute script that takes table as input

I want to learn Org Babel so I'm going through Code Blocks in Org.
I'm trying to execute the script in the basic tutorial and for some reason can't do it.
I'm supposed to feed a table as input to a function. This is the table:
#+name: tbl-example-data()
#+begin_src R
runif(n=5, min=0, max=1)
#+end_src
#+RESULTS: tbl-example-data
| 0.607781215803698 |
| 0.157157169422135 |
| 0.675619817571715 |
| 0.0488600700628012 |
| 0.998780139256269 |
The next function is the function to be executed:
#+name: R-mean(x)
#+begin_src R
mean(x)
#+end_src
When I try to run it by pressing C-c C-c I get
Variable "x" in block "R-mean" must be assigned a default value.
I tried to explicitly run the R-mean(x) function like so:
#+call: R-mean(tbl-example-data)
Unfortunately I get:
Reference 'R-mean' not found in this buffer.
How do I run the R-mean function using the values from tbl-example-data?
Thanks in advance.
Jenia
Okay I changed the code as per instructions:
#+NAME: tbl-example-data()
#+BEGIN_SRC R :results value <-------------------- changed
runif(n=5, min=0, max=1)
#+END_SRC
#+RESULTS: tbl-example-data
| 0.565871287835762 |
| 0.457158328965306 |
| 0.0498181856237352 |
| 0.988381117349491 |
| 0.898329895688221 |
#+NAME: R-mean(x)
#+BEGIN_SRC R :var x=tbl-example-data <-------------- changed
mean(x)
#+END_SRC
#+call: R-mean(x=tbl-example-data)
It returns me this:
Reference 'R-mean' not found in this buffer.
I dont know what I'm doing wrong.
You only need to make a couple of small changes in order to make this work.
Add :results value to the header of your first code block:
#+NAME: tbl-example-data()
#+BEGIN_SRC R :results value
runif(n=5, min=0, max=1)
#+END_SRC
#+RESULTS: tbl-example-data
...
Add :var x=tbl-example-data to the header of your second code block:
#+NAME: R-mean(x)
#+BEGIN_SRC R :var x=tbl-example-data
mean(x)
#+END_SRC
With these changes in place, both ways you describe for obtaining the final result will work. Evaluating the second code block via C-c C-c gives:
#+RESULTS: R-mean
: 0.49763968242332357
And calling the R-mean function via #+CALL: R-mean(tbl-example-data) produces:
#+RESULTS:
: 0.49763968242332357
Sources
Capturing the Results of Code Evaluation
Arguments to Code Blocks
I know this is a very old question, but I wanted to add this in case it will be useful for others. itsjeyd's answer didn't fully work for me either, and I believe this is why.
There are three changes that need to be made.
As itsjeyd pointed out, you need a default value for x in your function R-mean. You can put :var x=tbl-example-data, but you could also just put :var x=0.
You need to remove change #+NAME: R-mean(x) to #+NAME: R-mean.
When you pass x it's being read as a one-column data frame, not a vector (for me at least). So you may need to change mean(x) to mean(x[, 1]).
For me, the first code block returns a table regardless of whether I use :results value. Your mileage may vary.
This works for me.
#+NAME: tbl-example-data()
#+BEGIN_SRC R
runif(n=5, min=0, max=1)
#+END_SRC
#+RESULTS: tbl-example-data
| 0.926858797203749 |
| 0.830021075671539 |
| 0.788423302117735 |
| 0.794694420881569 |
| 0.943031084956601 |
#+NAME: R-mean
#+BEGIN_SRC R :var x=""
mean(x[, 1])
#+END_SRC
#+call: R-mean(x=tbl-example-data)
#+RESULTS:
: 0.856605736166239
One final note. If you have parentheses at the end of your first block name, #+NAME: tbl-example-data(), this block appears not to be evaluate anew each time. So running the call statement multiple times produces the same result.
But if you remove the parentheses, giving #+NAME: tbl-example-data, the block is evaluate each time the call statement is run, producing a different result each time. I didn't know this.
Hope that helps!

Org (version 7.9) converts periods and hyphens in tables to 0

I am using the Org mode that comes with Emacs 24.3, and I am having an issue that when Org creates a table from the result of a code block it is replacing characters like '-' and '.' with 0 (integer zero). Then when I pass the table to another code block that's expecting a column of strings I get type errors etc.
I haven't been able to find anything useful, as it seems to be practically un-Googleable. Has anyone had the same problem? If I update to the latest version of org-mode, will that fix it?
EDIT:
I updated to Org 8.2 and this problem seems to have gone away. Now I have another (related) problem, where returning a table with a cell containing a string consisting of one double quote character ('"' in python) messes something up; Org added 2 extra columns to the table, one had something like
(quote (quote ) ())
in it. The reason my tables have things like this in them is that I'm working with part-of-speech tags from natural language data.
It's pretty obvious Org is doing some stuff to try to interpret the table contents, and not dealing well with meta characters. Technically I think these are bugs where Org should be dealing better with unexpected input.
EDIT 2:
Here is a minimal reproduction with Org 7.9.3f (system Python is 3.4):
#+TBLNAME: table
| DT | The |
| . | . |
| - | - |
#+BEGIN_SRC python :var table=table
return table
#+END_SRC
#+RESULTS:
| DT | The |
| 0 | 0 |
| 0 | 0 |
Incidentally, Org does not like the '"' character at all, in tables or in code blocks (I just get a "End of file during parsing" message when the above table has a cell with just '"' in it). It's probably just better to avoid it altogether, so I think my problem is solved. If nobody wants to add anything, I'll answer this myself in a day or so.

Sum up several files in emacs?

I have roughly a gazillion files of the same architecture. Is there a way to create a buffer which will present a summary of those files? possibly with org-mode?
Each file is formated as:
q val counts
1 0.05 2500
4 0.01 2500
10 0.002 2500
.
.
.
.
The files are each in their own folder:
prog
|
+fold1
| |
| ----file1
+fold2
| |
| ----file1
-fold3
|
----file1
I'm not certain what should the summary conclude. I think that the first 3 lines + the averages of each file.
Expanding Juancho's comment, in org-mode, you can define a shell source block :
#+begin_src sh :results output raw replace
for i in /path/to/files/; do
head -n 3 $i
# I let you compute the average you want using awk and whatnot
done
#+end_src
and execute it with C-c C-c (cursor being in the block).
You may want to read the manual concerning header arguments if you want the results to be formatted in some particular fashion. (And you can do that in any language org-mode supports.)