[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC] ob-reticulate: R+Python interface from Babel

From: Jack Kamm
Subject: [RFC] ob-reticulate: R+Python interface from Babel
Date: Mon, 24 Aug 2020 08:26:40 -0700

Hi all,

Reticulate is an R package for interfacing between R and Python. It allows 
accessing objects in a Python session from R and vice versa. See 
https://rstudio.github.io/reticulate/ for more info about it.

I've written a small patch for using reticulate from org-babel. It allows 
creating a source block of lang "reticulate", which behaves as Python for font 
highlighting and editing, but is executed in an R session via reticulate.

I'm wondering whether this should go into org-mode, or whether to package this 
separately. I'm also curious whether this would be useful to anyone here. Any 
feedback is appreciated.

The main advantage of reticulate is being able to access Python objects 
directly from R and vice versa, without having to write them to a separate file 
or pass them through the ":var" header argument. For example, we could do the 

#+begin_src reticulate :session
  import pandas as pd

  fib = [0, 1]
  for _ in range(10):
      fib.append(fib[-1] + fib[-2])

  df = pd.DataFrame({
      "i": list(range(len(fib))),
      "F_i": fib

#+begin_src R :session :results graphics value file :file fig.png
  with(py$df, plot(i, F_i))

Reticulate source blocks support both "value" and "output" results, and even 
supports graphics with matplotlib. It's primarily intended to be used in 
sessions, and the ":session" header argument should match between reticulate 
and R source blocks.


>From 0f691a200cf088c72f93f7552d73caeafb8d588f Mon Sep 17 00:00:00 2001
From: Jack Kamm <jackkamm@gmail.com>
Date: Mon, 24 Aug 2020 08:02:17 -0700
Subject: [PATCH] ob-reticulate: Babel source lang for R+Python reticulate

* 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
+;; 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 <https://www.gnu.org/licenses/>.
+;;; 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) "
+            (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))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]