1 ;;; ob-stan.el --- Babel Functions for Stan -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2017 Free Software Foundation, Inc.
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; Org-Babel support for evaluating Stan [1] source code.
28 ;; Evaluating a Stan block can produce two different results.
30 ;; 1) Dump the source code contents to a file.
32 ;; This file can then be used as a variable in other blocks, which
33 ;; allows interfaces like RStan to use the model.
35 ;; 2) Compile the contents to a model file.
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".
41 ;; For more information and usage examples, visit
42 ;; http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-stan.html
44 ;; [1] http://mc-stan.org/
50 (defcustom org-babel-stan-cmdstan-directory nil
51 "CmdStan source directory.
52 'make' will be called from this directory to compile the Stan
53 block. When nil, executing Stan blocks dumps the content to a
58 (defvar org-babel-default-header-args
:stan
59 '((:results .
"file")))
61 (defun org-babel-execute:stan
(body params
)
62 "Generate Stan file from BODY according to PARAMS.
63 A :file header argument must be given. If
64 `org-babel-stan-cmdstan-directory' is non-nil and the file name
65 does not have a \".stan\" extension, save an intermediate
66 \".stan\" file and compile the block to the named file.
67 Otherwise, write the Stan code directly to the named file."
68 (let ((file (expand-file-name
69 (or (cdr (assq :file params
))
70 (user-error "Set :file argument to execute Stan blocks")))))
71 (if (or (not org-babel-stan-cmdstan-directory
)
72 (string-match-p "\\.stan\\'" file
))
73 (with-temp-file file
(insert body
))
74 (with-temp-file (concat file
".stan") (insert body
))
75 (let ((default-directory org-babel-stan-cmdstan-directory
))
76 (call-process-shell-command (concat "make " file
))))
77 nil
)) ; Signal that output has been written to file.
79 (defun org-babel-prep-session:stan
(_session _params
)
80 "Return an error because Stan does not support sessions."
81 (user-error "Stan does not support sessions"))
84 ;;; ob-stan.el ends here