GNU Emacs 26.0.50.2 (x86_64-pc-linux-gnu, GTK+ Version 3.18.9) of 2016-10-06
Org-mode version 8.3.5 (release_8.3.5-1131-g7eecb2 @ ~/.emacs.d/el-get/org-mode/lisp/)
I have come to share my projects as html files where I include notes and codes so that others can follow my workflow and reproduce my results.
One thing is that I want to reuse code blocks with different input variables, and I do this with the :var
header arg. The issue is that :var
arguments are not included in the exported code block.
Is it possible to selectively choose code blocks where the :var
arguments are included in the exported code block?
I noticed that when tangling the code block the variables are included in the file.
Note that I am asking for a language agnostic solution if possible.
Example use case:
cat ./export_var.org
* Export with variables
:PROPERTIES:
:header-args: :var status="not_finished"
:END:
#+NAME: example-table
| 1 |
| 2 |
| 3 |
| 4 |
Here we import a table.
#+NAME: table-length
#+BEGIN_SRC R :var table=example-table :tangle "./export_var.R"
status <- "finished"
dim(table)
#+END_SRC
#+RESULTS: table-length
| 4 |
| 1 |
Here we think we import a table
#+NAME: table-str-length
#+BEGIN_SRC R :var table="./filename_example-table.tsv" :tangle "./export_var.R"
length(table)
print(status)
#+END_SRC
#+RESULTS: table-str-length
: not_finished
** Some subcase
If I run with the data in ="./other_data.tsv"=
#+call: table-str-length[ :var table="./other_data.tsv"]()
#+RESULTS:
: not_finished
Now this would tangle as the following ./export_var.R
as
status <- "not_finished"
table <- local({
con <- textConnection(
"\"1\"
\"2\"
\"3\"
\"4\""
)
res <- utils::read.table(
con,
header = FALSE,
row.names = NULL,
sep = "\t",
as.is = TRUE
)
close(con)
res
})
status <- "finished"
dim(table)
status <- "not_finished"
table <- "./filename_example-table.tsv"
length(table)
print(status)
The desired result would be the same code as in the tangled file, although split up as usual in the html file.
The inheritance structure of :header-args
can be somewhat complex.
org-babel-expand-src-block
as a template to enhance:export
header. To seeorg-babel-expand-src-block
in action, click inside your code block and typeC-c C-v C-v
and your code will/should be displayed with variables in a new frame. Unfortunately, I can't figure out how to capture the expanded buffer and insert it back into the org file. I'll keep looking though.org-babel-expand-src-block
contained the answer! I'm writing up some language agnostic examples for a new answer. Thank you so much for asking the question!