From 0f691a200cf088c72f93f7552d73caeafb8d588f Mon Sep 17 00:00:00 2001 From: Jack Kamm Date: Mon, 24 Aug 2020 08:02:17 -0700 Subject: [PATCH] ob-reticulate: Babel source lang for R+Python reticulate package * lisp/ob-reticulate.el: New babel source block lang for R's reticulate package for evaluating Python code. * lisp/org-src.el (org-src-lang-modes): Add reticulate. --- lisp/ob-reticulate.el | 50 +++++++++++++++++++++++++++++++++++++++++++ lisp/org-src.el | 1 + 2 files changed, 51 insertions(+) create mode 100644 lisp/ob-reticulate.el diff --git a/lisp/ob-reticulate.el b/lisp/ob-reticulate.el new file mode 100644 index 000000000..7da48681c --- /dev/null +++ b/lisp/ob-reticulate.el @@ -0,0 +1,50 @@ +;;; ob-reticulate.el --- Babel Functions for reticulate -*- lexical-binding: t; -*- + +;; Copyright (C) 2020 Free Software Foundation, Inc. + +;; Author: Jack Kamm +;; Keywords: literate programming, reproducible research, R, statistics +;; Homepage: https://orgmode.org + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Commentary: + +;; Org-Babel support for the R package reticulate. + +;;; Code: + +(require 'ob-R) +(require 'ob-python) + +(defalias 'org-babel-edit-prep:reticulate 'org-babel-edit-prep:R) + +(defun org-babel-execute:reticulate (body params) + (let* ((tmp-src-file (org-babel-temp-file "reticulate-")) + (result-type (cdr (assq :result-type params)))) + (with-temp-file tmp-src-file (insert body)) + (org-babel-execute:R + (format (concat "reticulate::py_run_string(\"%s\")" + (when (equal result-type 'value) " +reticulate::py$`__org_babel_python_final`")) + (format org-babel-python--eval-ast + (org-babel-process-file-name + tmp-src-file 'noquote))) + params))) + +(provide 'ob-reticulate) + +;;; ob-reticulate.el ends here diff --git a/lisp/org-src.el b/lisp/org-src.el index 28733d011..1b3d83f87 100644 --- a/lisp/org-src.el +++ b/lisp/org-src.el @@ -197,6 +197,7 @@ (defcustom org-src-lang-modes ("dot" . fundamental) ("elisp" . emacs-lisp) ("ocaml" . tuareg) + ("reticulate" . python) ("screen" . shell-script) ("shell" . sh) ("sqlite" . sql)) -- 2.28.0