commit | author | age
|
76bbd0
|
1 |
;;; ob-stan.el --- Babel Functions for Stan -*- lexical-binding: t; -*- |
C |
2 |
|
|
3 |
;; Copyright (C) 2015-2018 Free Software Foundation, Inc. |
|
4 |
|
|
5 |
;; Author: Kyle Meyer |
|
6 |
;; Keywords: literate programming, reproducible research |
|
7 |
;; Homepage: https://orgmode.org |
|
8 |
|
|
9 |
;; This file is part of GNU Emacs. |
|
10 |
|
|
11 |
;; GNU Emacs is free software: you can redistribute it and/or modify |
|
12 |
;; it under the terms of the GNU General Public License as published by |
|
13 |
;; the Free Software Foundation, either version 3 of the License, or |
|
14 |
;; (at your option) any later version. |
|
15 |
|
|
16 |
;; GNU Emacs is distributed in the hope that it will be useful, |
|
17 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19 |
;; GNU General Public License for more details. |
|
20 |
|
|
21 |
;; You should have received a copy of the GNU General Public License |
|
22 |
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. |
|
23 |
|
|
24 |
;;; Commentary: |
|
25 |
|
|
26 |
;; Org-Babel support for evaluating Stan [1] source code. |
|
27 |
;; |
|
28 |
;; Evaluating a Stan block can produce two different results. |
|
29 |
;; |
|
30 |
;; 1) Dump the source code contents to a file. |
|
31 |
;; |
|
32 |
;; This file can then be used as a variable in other blocks, which |
|
33 |
;; allows interfaces like RStan to use the model. |
|
34 |
;; |
|
35 |
;; 2) Compile the contents to a model file. |
|
36 |
;; |
|
37 |
;; This provides access to the CmdStan interface. To use this, set |
|
38 |
;; `org-babel-stan-cmdstan-directory' and provide a :file argument |
|
39 |
;; that does not end in ".stan". |
|
40 |
;; |
|
41 |
;; For more information and usage examples, visit |
|
42 |
;; https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-stan.html |
|
43 |
;; |
|
44 |
;; [1] http://mc-stan.org/ |
|
45 |
|
|
46 |
;;; Code: |
|
47 |
(require 'ob) |
|
48 |
(require 'org-compat) |
|
49 |
|
|
50 |
(defcustom org-babel-stan-cmdstan-directory nil |
|
51 |
"CmdStan source directory. |
|
52 |
Call \"make\" from this directory to compile the Stan block. |
|
53 |
When nil, executing Stan blocks dumps the content to a file." |
|
54 |
:group 'org-babel |
|
55 |
:type '(choice |
|
56 |
(directory :tag "Compilation directory") |
|
57 |
(const :tag "Dump to a file" nil))) |
|
58 |
|
|
59 |
(defvar org-babel-default-header-args:stan |
|
60 |
'((:results . "file"))) |
|
61 |
|
|
62 |
(defun org-babel-execute:stan (body params) |
|
63 |
"Generate Stan file from BODY according to PARAMS. |
|
64 |
A :file header argument must be given. If |
|
65 |
`org-babel-stan-cmdstan-directory' is non-nil and the file name |
|
66 |
does not have a \".stan\" extension, save an intermediate |
|
67 |
\".stan\" file and compile the block to the named file. |
|
68 |
Otherwise, write the Stan code directly to the named file." |
|
69 |
(let ((file (expand-file-name |
|
70 |
(or (cdr (assq :file params)) |
|
71 |
(user-error "Set :file argument to execute Stan blocks"))))) |
|
72 |
(if (or (not org-babel-stan-cmdstan-directory) |
|
73 |
(string-match-p "\\.stan\\'" file)) |
|
74 |
(with-temp-file file (insert body)) |
|
75 |
(with-temp-file (concat file ".stan") (insert body)) |
|
76 |
(let ((default-directory org-babel-stan-cmdstan-directory)) |
|
77 |
(call-process-shell-command (concat "make " file)))) |
|
78 |
nil)) ; Signal that output has been written to file. |
|
79 |
|
|
80 |
(defun org-babel-prep-session:stan (_session _params) |
|
81 |
"Return an error because Stan does not support sessions." |
|
82 |
(user-error "Stan does not support sessions")) |
|
83 |
|
|
84 |
(provide 'ob-stan) |
|
85 |
;;; ob-stan.el ends here |