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

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!

Related

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.

Multiple org-mode tables sharing one #+TBLFM line

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

org-mode babel python session and inlined image incompatible?

If I use this block
#+BEGIN_SRC python :results file
from pylab import *
plot(rand(10))
savefig('images/test.png')
return 'images/test.png'
#+END_SRC
then the RESULTS block shows me an inlined version of the plot.
If now I switch to this block
#+BEGIN_SRC python :session test :results file
from pylab import *
plot(rand(10))
savefig('images/test.png')
return 'images/test.png'
#+END_SRC
then the RESULTS block does not show the inlined plot but this
| <matplotlib.lines.Line2D | object | at | 0x35c0650> |
Using a session is kind of mandatory for me because I need several blocks to share variables.
Is there something evidently wrong with my approach?
As per the org-mode documentation, you have to drop the return if the code is running in a session.
#+BEGIN_SRC python :session test :results file
from pylab import *
plot(rand(10))
savefig('images/test.png')
'images/test.png'
#+END_SRC
#+RESULTS:
[[file:images/test.png]]
because "the result returned is the result of the last evaluation performed by the interpreter."

How to import/expand noweb refs in evaluated (exported) source blocks in org-babel?

I'm trying to do something like this:
* Define some functions
#+begin_src python :noweb_ref defs
def f1(a,b,c):
return True
def f2(d,e,f):
return False
#+end_src
* Use them in a results-exported block later
#+begin_src python :results output :exports both :tangle yes
<<defs>>
print "test results:"
print f1(1,2,3)
#end_src
What I want to happen is for <<defs>> to be expanded tangle-style when the block is evaluated to produce the export output. What actually happens is that <<defs>> gets evaluated literally and causes a syntax error.
When tangling blocks like this to an output file, everything works perfectly, but I can't figure out how to do the same thing when I'm exporting the buffer.
Suggestions?
I'm not sure to really understand your point... but
1) you miss a noweb:yes header argument
2) you can use <<func()>> to insert the results of evaluating func (instead of the code of func) -- that's here that I'm not sure about what you really want.
You can also use :noweb no-export. That shows the noweb-syntax in exported files but expands the code blocks when evaluating or tangling the files.
:noweb strip-export is great if you just want to show an algorithm:
<<prep>>
result = A + B
<<plot>>
The exported file then shows this:
result = A + B

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