How to include an emacs table in tangled output? - emacs

I'd like to insert a table into tangled output as a comment. Using the table name yields a blank result:
#+name: test-data
| type | amount |
|--------+--------|
| sale | 31.41 |
| return | 5.92 |
#+BEGIN_SRC python :var data=test-data :tangle test.py :colnames no :noweb yes
## Table
## <<test-data>>
[zip(data[0], row) for row in data[1:]]
#+END_SRC
output:
data=[["type", "amount"], ["sale", 31.41], ["return", 5.92]]
## Table
##
[zip(data[0], row) for row in data[1:]]
Calling the reference yields a lisp list:
#+BEGIN_SRC python :var data=test-data :tangle test.py :colnames no :noweb yes
## Table
## <<test-data()>>
#+END_SRC
...
## Table
## (("type" "amount") hline ("sale" 31.41) ("return" 5.92))

You can just about do what you want by wrapping up your table in its own code block. The slight disadvantage is the extra boilerplate and the extra line in the comment:
#+name: test-data-block
#+BEGIN_SRC org
#+name: test-data-table
| type | amount |
|--------+--------|
| sale | 31.41 |
| return | 5.92 |
#+END_SRC
#+BEGIN_SRC python :var data=test-data-table :tangle test.py :colnames no :noweb yes
## Table
## <<test-data-block>>
[zip(data[0], row) for row in data[1:]]
#+END_SRC
And the tangled output is:
data=[["type", "amount"], ["sale", 31.41], ["return", 5.92]]
## Table
## #+name: test-data-table
## | type | amount |
## |--------+--------|
## | sale | 31.41 |
## | return | 5.92 |
[zip(data[0], row) for row in data[1:]]

Related

org-babel not concatenating strings before sending to code block variable.

I have just started using org-mode and org-babel as a lab notebook. I am trying to use a code block to fill in two columns of a table. The code block seems to work for the first column because those are the right numbers. However, when trying to concatenate a string to the file name in column three so the code blocks works on a different set of files it seems to just run the code block on the original files instead, which produces the same output as column one.
#+name: tRNAs
#+begin_src sh :var filename="" :results silent
cd Data/tRNA
grep -c ">" $filename
#+end_src
#+tblname: sequences
| # of Sequences before QC | # after QC | Original File name|
|--------------------------+------------+------------------|
| 681865 | 681865 | read1 |
| 324223 | 324223 | read2 |
| 1014578 | 1014578 | read3 |
| 971965 | 971965 | read4 |
| 931777 | 931777 | read5 |
| 810798 | 810798 | read6 |
| 965134 | 965134 | read7 |
| 718474 | 718474 | read8 |
|--------------------------+------------+------------------|
#+TBLFM: $1='(org-sbe tRNAs (filename (concat "\"" $3 "\"")))
#+TBLFM: $2='(org-sbe tRNAs (filename (concat "\"final_" $3 "\"")))

How do I tag a table in org-mode?

I've got a huge table for R which should not be exported. So I added a #TAGS: noexport above the table, but that doesn't seem to have any effect. So how do I tag a table?
You cannot add tags to a table, only to a header.
Also, the #+TAGS directive is used to declare a group of tags specific to the whole document, not for tagging a table.
If you surround the table within a comment block, it will not be exported. For example:
#+begin_comment
#+tblname: very_long_table
| col1 | col2 |
|------+------|
| 1 | 22 |
| 2 | 23 |
| 3 | 24 |
| 4 | 25 |
| 5 | 26 |
#+end_comment
#+begin_src sh :var tbl=very_long_table :exports both
echo $tbl
#+end_src
#+RESULTS:
: 1 22 2 23 3 24 4 25 5 26

Convert between an org-mode table and a table.el table without user interaction

I convert org-mode table to table.el table. For that I select the table:
| Option | Type | Value | Descr |
| -[no]h | bool | yes | Print |
| -[no]versio | bool | no | Print |
| -nice | int | 0 | Set t |
| -[no]v | bool | no | Be lo |
| -time | real | -1 | Take |
| -[no]rmvsbd | bool | yes | Removvirtual |
| sites | | | |
| -maxwarn | int | 0 | Numbe |
| procenerate | | | |
| unsta | | | |
| -[no]zero | bool | no | Set pthout |
| defau error | | | |
| -[no]renum | bool | yes | Renum |
| atomty | | | |
and press C-c ~. org-mode then asks me
Convert table to table.el table? (y or n)
How do I answer y programmically? I read the docs of that defun -- there's not way to do it with prefix arg.
Similar functionality in bash:
echo y | script-which-asks-y-or-n
C-c ~ calls the command org-table-create-with-table.el, which provides a bunch of wrappers around calling org-table-convert. If you want to use this function when you know you are already in an org-mode table, you don't need the wrappers, you just need the two (and probably only one) commands: org-table-align and org-table-convert.
So if you're doing this interactively, you can just call M-x org-table-convert and you're done. This assumes the table is already aligned. You can do this by hand by tabbing from one cell to the next, which triggers table alignment. Or you can do it with a small function:
(defun my-convert-tables ()
"No questions asked, just convert the table"
(interactive)
(org-table-align)
(org-table-convert))
You can do this programmatically as follows. You would replace the function name test1 with org-table-create-with-table.el in your defadvice functions that would be otherwise the same as those below.
Using defadvice to run some code before and after the function, we can save the function bound to the symbol y-or-n-p to a global variable and rebind it to a function that simply returns true. After the function we then restore the original functionality.
(setq save-y-or-n-p nil)
(defadvice test1 (around always-yes)
(fset 'save-y-or-n-p (symbol-function 'y-or-n-p))
(fset 'y-or-n-p (lambda (s) t))
ad-do-it
(fset 'y-or-n-p (symbol-function 'save-y-or-n-p)))
(defun test1 ()
(interactive)
(if (y-or-n-p "Happy? ")
(insert "Happy day")
(insert "Unhappy day")))

Table formula for org spreadsheet

I have a table as follow:
| Xn | S | Pn |
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 1 | 0 |
I would like to search through columns Xn and S and return the value of Pn for which Xn=1 and S=0.
Can anyone advise on how I could go about doing this?
#+tblname: example-table
| Xn | S | Pn |
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 1 | 0 |
#+source: compute-table
#+begin_src emacs-lisp :var table=example-table
(require 'cl)
(loop for (xn s pn) in (rest table)
when (and (= xn 1) (= s 0)) collect pn)
#+end_src
#+results: compute-table
| 1 |
Use org-babel: Name the table and use it as input for a function that does the search in a language of your choice (out of the many languages supported by org).
In pseudo code:
#+tblname: my_table
|Xn|S|Pn|
| 0|0|9 |
[...]
#+name filter_table
#+begin_src lang :var tbl=my_table :results output
filter tbl # tbl (my_table by default) is passed in as array of arrays (or list of lists)
print matching Pn
#+end_src

Org-mode tables: Exclude columns from export

I have a set of tables in org-mode that I am exporting, but I'd like certain columns used for calculations and consumption by code blocks to be excluded from LaTeX export.
I'm sure I saw a way to do this by specifying a range of columns to export below the table, but I can't find reference to it anywhere on the web so there's a good chance I dreamt it.
Another way to achieve this is by defining a "hidden" column type H in the LaTeX header options of the org file and then use #+ATTR_LATEX: :align llH to indicate that the third column is to be hidden on export (source):
#+LATEX_HEADER: \usepackage{array}
#+LATEX_HEADER: \newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}#{}}
#+ATTR_LATEX: :align llH
|-----+-------+------|
| 2 | 1/2 | junk |
| 4 | 1/4 | junk |
| 8 | 1/2 | junk |
If you are using "Radio Tables" you can do something like
#+ORGTBL: SEND some-name orgtbl-to-latex :skipcols (3)
|-----+-------+------|
| 2 | 1/2 | junk |
| 4 | 1/4 | junk |
| 8 | 1/2 | junk |
See http://www.gnu.org/software/emacs/manual/html_mono/org.html#Radio-tables for all the details.
I believe it may not be possible directly with export via C-c C-e since they offer the same answer at http://comments.gmane.org/gmane.emacs.orgmode/33946 from November 2010.
I use Michael Brand's solution proposed here and cataloged by Derek Feichtinger here (make sure to view the file in raw mode, otherwise the source is hidden by GitHub).
For convenience, I reproduce the code below:
* Exporting tables with some columns hidden
It is desirable to be able and hide columns in exported output. This is often the
case in tables where a lot of computations are done, and where intermediate
results end up in columns that one does not want to end up in the exported document.
This functionality is currently not available by standard org, but since this is Emacs, a simple function
implementing this functionality was published by [[https://github.com/brandm][Michael Brand]] within this [[http://lists.gnu.org/archive/html/emacs-orgmode/2016-05/msg00027.html][emacs-orgmode thread]].
#+BEGIN_SRC emacs-lisp :results silent :exports source
(defun dfeich/org-export-delete-commented-cols (back-end)
"Delete columns $2 to $> marked as `<#>' on a row with `/' in $1.
If you want a non-empty column $1 to be deleted make it $2 by
inserting an empty column before and adding `/' in $1."
(while (re-search-forward "^[ \t]*| +/ +|\\(.*|\\)? +\\(<#>\\) *|" nil t)
(goto-char (match-beginning 2))
(org-table-delete-column)
(beginning-of-line)))
(add-hook 'org-export-before-processing-hook #'dfeich/org-export-delete-commented-cols)
;; (remove-hook 'org-export-before-processing-hook #'dfeich/org-export-delete-commented-cols)
#+END_SRC
The exported table will have col2 removed.
| | col1 | col2 | col3 |
| / | <r> | <#> | |
| | a1 | a2 | a3 |
| | b1 | b2 | b3 |
http://www.gnu.org/software/emacs/manual/html_mono/org.html#The-spreadsheet
3.5.6 Editing and debugging formulas
Use ‘/’ for: Do not export this line. Useful for lines that contain the narrowing ‘’ markers or column group markers.
Note to do this you must use the first column for extra info to do this.
I've found that the most convenient way is to:
make the table not exportable with :noexport: tag,
select the columns I want to be exported with source block, using elisp, and make the result exportable.
Here is an example with elisp.
* Hidden :noexport:
#+NAME: google
| file | total | other | p |
|-----------+-------+-------+----|
| de-01.pdf | 312 | 76 | 76 |
| de-02.pdf | 428 | 101 | 77 |
| de-03.pdf | 1069 | 217 | 80 |
* Exported
Here it comes.
#+begin_src elisp :var data=google :colnames yes
;; select 0th and 3rd column from a table accessible with 'google' name
;; and do some math on it
(mapcar (lambda (e) (list (nth 0 e) (nth 3 e))) data)
#+end_src
#+RESULTS:
| file | p |
|-----------+----|
| de-01.pdf | 76 |
| de-02.pdf | 77 |
| de-03.pdf | 80 |
You can also use any other language that is supported by the source blocks to loop over the results and produce desired output.