Redirect sereral functions outputs to results - redirect

When work in source code of python,
#+BEGIN_SRC python :session test :results output
print('testing1')
print('testing2')
#+END_SRC
#+RESULTS:
: testing1
: testing2
Set :results as output and then get 2 results,
Try elisp
#+begin_src emacs-lisp :results output
(+ 21 35 12 7)
(* 25 4 12)
#+end_src
#+RESULTS:
How could get elisp codes redirect outputs to results

Those source blocks have different behaviour -- your python code prints to stdout while the elisp just evaluates expressions.
An equivalent elisp block might be
#+BEGIN_SRC elisp :results output
(princ (+ 21 35 12 7))
(print (* 25 4 12))
#+END_SRC
#+RESULTS:
: 75
: 1200
If you want to capture the results of both expressions, you could wrap them in a list,
#+BEGIN_SRC elisp :results value verbatim
(list (+ 1 1) (* 2 2))
#+END_SRC
#+RESULTS:
: (2 4)
#+BEGIN_SRC python :session test :results value verbatim
1 + 1, 2 + 2
#+END_SRC
#+RESULTS:
: (2, 4)

Related

Org-babel does not print results from Anaconda in src block

I need some help to configure properly anaconda to run python3 code inside org-mode files with babel.
Somehow when I try to run code from libraries in Anaconda, babel does not recognize it. Here is the results that I get from the code run in org-mode
#+BEGIN_SRC python :session :results value
import pandas as pd
my_df = pd.DataFrame({'Col1': [2, 7, 9], 'Col2': [1, 6, 12], 'Col3': [1, 6, 9]})
my_df
#+END_SRC
#+RESULTS:
: <_ast.Expr object at 0x7f20e28f5c50>
And the iPython terminal returns:
In [7]: 'org_babel_python_eoe'
In [7]: Out[7]: 'org_babel_python_eoe'
The corresponding details of my init file are here
;; ELPY
(package-initialize)
(elpy-enable)
;; Initialise anaconda
(setq python-shell-virtualenv-path "/home/teoten/anaconda3/bin/python3")
;; iphython
(setq python-shell-interpreter "/home/teoten/anaconda3/bin/ipython3"
python-shell-interpreter-args "-i --simple-prompt")
;; Set org-babel
(setq org-confirm-babel-evaluate nil)
(org-babel-do-load-languages
'org-babel-load-languages
'((R . t)
(python . t)
(lisp . t)
(latex . t)))
What am I doing wrong? Is it the virtualenv-path? I already tried different options with no success

Passing org-mode properties to source code block via org-sbe

I could not figure out how to pass the property RID to a source code block in a different scope, e.g. it fails to evaluate (org-entry-get nil "RID") before it is passed to the function addSomething. It does work when using #+CALL:, but the same syntax is not working in a SRC block (see last example below).
#+NAME: addSomething
#+BEGIN_SRC sh :results value :var x="no"
echo "something: $x"
#+END_SRC
* Heading 1
:PROPERTIES:
:RID: h1_property
:END:
This works.
#+BEGIN_SRC sh :var y=(org-sbe addSomething (x "1"))
echo $y
#+END_SRC
#+RESULTS:
: something: 1
This works too:
#+BEGIN_SRC sh :var y=(org-entry-get nil "RID")
echo $y
#+END_SRC
#+RESULTS:
: h1_property
Error: Reference 'RID' not found in this buffer
#+BEGIN_SRC sh :var y=(org-sbe addSomething (org-entry-get nil "RID"))
echo $y
#+END_SRC
Error: Reference 'just a string' not found in this buffer.
#+BEGIN_SRC sh :var y=(org-sbe addSomething (x "a string"))
echo $y
#+END_SRC
Why? Passing "1" worked.
Error: Symbol's variable is void: RID
#+BEGIN_SRC sh :var y=(org-sbe addSomething (x (org-entry-get nil "RID")))
echo $y
#+END_SRC
Seems ~(org-entry-get nil "RID")~ is evaluated outside of the current scope.
It works using CALL.
#+CALL: addSomething(x=(org-entry-get nil "RID")) :results value
#+RESULTS:
: something: h1_property
Try the same for `:var`:
#+BEGIN_SRC sh :var y=addSomething(x=(org-entry-get nil "RID"))
echo $y
#+END_SRC
#+RESULTS:
: something:
For the string case, try this:
#+BEGIN_SRC sh :var y=(org-sbe addSomething (x $"a string"))
echo $y
#+END_SRC
#+RESULTS:
: something: a string
For the RID case, try this:
#+BEGIN_SRC sh :var y=(org-sbe addSomething (x (org-entry-get nil \"RID\")))
echo $y
#+END_SRC
#+RESULTS:
: something: h1_property
You can add source blocks to calculate whatever elements you want and then use org-sbe to pass the results to other source blocks; e.g.
#+name: rid
#+BEGIN_SRC sh :var y=(org-entry-get nil "RID")
echo $y
#+END_SRC
#+BEGIN_SRC sh :var y=(org-sbe addSomething (x (org-sbe rid)))
echo $y
#+END_SRC
#+RESULTS:
: something: h1_property
and similarly
#+name: string
#+BEGIN_SRC sh :var y="a string"
echo $y
#+END_SRC
#+RESULTS: string
: a string
#+BEGIN_SRC sh :var y=(org-sbe addSomething (x (org-sbe string)))
echo $y
#+END_SRC
#+RESULTS:
: something: a string
And here's the second example with the arguments passed inline (again using the "rid" source block defined above):
#+BEGIN_SRC sh :var y=addSomething((org-sbe rid)))
echo $y
#+END_SRC
#+RESULTS:
: something: h1_property
I realize that this is neither a complete nor a satisfactory answer, but I have not had the time or patience to figure out even a small subset (let alone the complete set) of the rules. A good answer would form the core of a very nice blog post that I, for one, would very much look forward to reading (hint, hint...)

How to disable Racket's any s-expression value output

Example, I write a Racket script:
; filename: hello.rkt
#lang racket/base
(displayln "hello")
(+ 1 1)
and execute it:
$ racket hello.rkt
output is:
hello
2
I don't want it to print the number '2', how to disable result of the any S-expression value output?
You could use void to discard values, and begin to group several expressions together. E.g.
(void (begin (+ 1 1) (+ 2 2) (+ 3 3)))
This prints nothing.

Org-Mode: call the noweb insertion from the noweb insertion

a and b -- python functions, c -- simple noweb insertion to python source-blocks, and d -- noweb insertion to function with noweb insertion. Function c -- is working, but function d -- not, because python trying execute insertions but this is invalid syntax. Is it possible to run noweb insertion from the noweb insertion?
#+NAME: a
#+BEGIN_SRC python
a = 1
#+END_SRC
#+NAME: b
#+BEGIN_SRC python
b = 1
#+END_SRC
#+NAME: c
#+BEGIN_SRC python :noweb yes :results output
<<a>>
<<b>>
c = a + b
print c
#+END_SRC
#+NAME: d
#+BEGIN_SRC python :noweb yes :results output
<<c>>
d = c * 2
print d
#+END_SRC
During the experiments, I found that this construction works (all noweb insertion must be in the last code block):
#+NAME: a
#+BEGIN_SRC python
a = 1
#+END_SRC
#+NAME: b
#+BEGIN_SRC python
b = 1
#+END_SRC
#+NAME: c
#+BEGIN_SRC python
c = a + b
#+END_SRC
#+NAME: d
#+BEGIN_SRC python :noweb yes :results output
<<a>>
<<b>>
<<c>>
d = c * 2
print d
#+END_SRC

Howto convert org-mode table to original tabbed format?

I am converting a region to a table by using C-c |.
Is there a way to reverse the process, say after converting do some editing and go back to original format (tab separated values will do)?
I know that I can do it via org-table-export but that is too cumbersome.
Try orgtbl-to-tsv for tab-separated values.
There is also orgtbl-to-csv for comma-separated values.
Combining the table with a short code block to do the conversion is convenient. For example:
* Some heading
#+name: foo
| a | b | c |
|---+---+---|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
#+name: foo-csv
#+BEGIN_SRC elisp :var x=foo :wrap example
(orgtbl-to-csv x nil)
#+END_SRC
#+RESULTS: foo-csv
#+begin_example
1,2,3
4,5,6
#+end_example
C-c C-c on the code block will produce the results shown. Adding :colnames no as a header argument to the code block will also preserve the header line:
#+name: foo-csv
#+BEGIN_SRC elisp :var x=foo :wrap example :results raw :colnames no
(orgtbl-to-csv x nil)
#+END_SRC
#+RESULTS: foo-csv
#+begin_example
a,b,c
1,2,3
4,5,6
#+end_example
I needed this too and just wrote the following based on org-table-export:
(defun org-table-transform-in-place ()
"Just like `ORG-TABLE-EXPORT', but instead of exporting to a
file, replace table with data formatted according to user's
choice, where the format choices are the same as
org-table-export."
(interactive)
(unless (org-at-table-p) (user-error "No table at point"))
(org-table-align)
(let* ((format
(completing-read "Transform table function: "
'("orgtbl-to-tsv" "orgtbl-to-csv" "orgtbl-to-latex"
"orgtbl-to-html" "orgtbl-to-generic"
"orgtbl-to-texinfo" "orgtbl-to-orgtbl"
"orgtbl-to-unicode")))
(curr-point (point)))
(if (string-match "\\([^ \t\r\n]+\\)\\( +.*\\)?" format)
(let ((transform (intern (match-string 1 format)))
(params (and (match-end 2)
(read (concat "(" (match-string 2 format) ")"))))
(table (org-table-to-lisp
(buffer-substring-no-properties
(org-table-begin) (org-table-end)))))
(unless (fboundp transform)
(user-error "No such transformation function %s" transform))
(save-restriction
(with-output-to-string
(delete-region (org-table-begin) (org-table-end))
(insert (funcall transform table params) "\n")))
(goto-char curr-point)
(beginning-of-line)
(message "Tranformation done."))
(user-error "Table export format invalid"))))
(define-key org-mode-map (kbd "\C-x |") 'org-table-transform-in-place)
It'd be great if this got added to org-mode proper as I think many would use it.
Mark the region.
M-x replace-string
|
C-q TAB RET
If you want to tweak it, use replace-regex.
Here are the steps to use export the table as tab or comma separated values:
Use the command org-table-export. M-x org-table-export
Enter the filename to save to (or hit enter for the same file).
Select the format (this is where you can set the orgtbl-to-tsv or any other formats).
These are some of the formats that can be used:
orgtbl-to-csv
orgtbl-to-generic
orgtbl-to-html
orgtbl-to-latex
orgtbl-to-orgtbl
orgtbl-to-texinfo
orgtbl-to-tsv