ob-stan.el 3.0 KB

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