emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Jack Kamm <jackkamm@gmail.com>
To: emacs-orgmode@gnu.org
Subject: [RFC] ob-reticulate: R+Python interface from Babel
Date: Mon, 24 Aug 2020 08:26:40 -0700	[thread overview]
Message-ID: <875z98gj4f.fsf@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1473 bytes --]

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 following:

#+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
  })
#+end_src

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

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.

Cheers,
Jack


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-reticulate-Babel-source-lang-for-R-Python-reticul.patch --]
[-- Type: text/x-patch, Size: 2845 bytes --]

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
 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 <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) "
+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


             reply	other threads:[~2020-08-24 15:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-24 15:26 Jack Kamm [this message]
2020-08-25 23:34 ` [RFC] ob-reticulate: R+Python interface from Babel Kyle Andrews
2020-08-29 13:23 ` Kyle Meyer
2020-09-04  7:14   ` Bastien
2020-10-12  4:15 ` Jack Kamm
2021-02-27 14:15   ` Jack Kamm
2021-02-27 17:24     ` Jeremie Juste
2021-03-02 23:52       ` Jack Kamm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=875z98gj4f.fsf@gmail.com \
    --to=jackkamm@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).