From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id ICBfKVzcQ1+nKQAA0tVLHw (envelope-from ) for ; Mon, 24 Aug 2020 15:27:24 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id eP4YJVzcQ1+iHwAAbx9fmQ (envelope-from ) for ; Mon, 24 Aug 2020 15:27:24 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id D3835940309 for ; Mon, 24 Aug 2020 15:27:23 +0000 (UTC) Received: from localhost ([::1]:50772 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAENW-0007Sn-Od for larch@yhetil.org; Mon, 24 Aug 2020 11:27:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35274) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAEMw-0007Qt-LT for emacs-orgmode@gnu.org; Mon, 24 Aug 2020 11:26:46 -0400 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]:41631) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kAEMu-00042I-Bl for emacs-orgmode@gnu.org; Mon, 24 Aug 2020 11:26:46 -0400 Received: by mail-pf1-x434.google.com with SMTP id t9so828369pfq.8 for ; Mon, 24 Aug 2020 08:26:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version; bh=SyyGaTJalg/Ew4yYqC0YzsKkyuVfCep94Y2R/UZCjtw=; b=qKPGTyXcFHiecWCmft79XB1BhTiT0MCsX/X4KA8Ei9Hy5IMPUZYdLcV+/DQ7Yv2E+t 2NPIW1bEleDocrrvzGh1O1t6Vjaic7kxv2qqHcERfyzpt6uEIScTxBUOSiFiPv+el0sD h/rwuGlJULmrXgp9SCCfD9CR9dLpyM630Ayy7qIw8lqGFMvysCItjXgDPmNr4kECE3uA F5XSC5UNMi8wkHfQpCmMnz1avXhnKqiUljqX+3pH4NcVvfJ3pm/3h+wxpBk0TQN+HsJO 3if990LLVLSDRk++Efud/y9gmEJwy255A+h2qY1t61n0hiZk66ZX+9gmffXoTCti48lM 7xeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=SyyGaTJalg/Ew4yYqC0YzsKkyuVfCep94Y2R/UZCjtw=; b=AvlFA3/J3TMPK9xTSR8n7FgMmvEmWWUHLX5x3cZONwwVXJSS5yMokAT/54qr5+tkBc 0XgYX4o20CKuyf6rajoHj7mvc//T2j6SQrJPeo9wRh6TYayyjTkjyGlnNYLxJ+EiIaw+ ptALlSqns3Tz3hgHVYO6BI4E2nb1MueCpF9uupPN0QkyJxSbItgAwLRVdhhTgIOOf4kE +KGruKb6M04l5ohQDuIEurMw6BDONFWTOK0bEvNffGrSMTczXSfxyePsF7bldD1oY2ji 2Y5XS71/aXg6whZFSbZe5RxikY0n8wZGOBqJCkdsv8LUY6Dy6g9QSzDKMFsL4Qx0BjPP nfPA== X-Gm-Message-State: AOAM531bgyNq79JZ3BsbMG9xco1QdObxwlGZQI8HplfWwLQBItwq5Phm 0TO6p4JwMwSIPevUTncjNnfA6zFC0MA= X-Google-Smtp-Source: ABdhPJxi1xiXu4rdlV70qj8VejGoAz4Wcdg5ct++Uf2CUpfkHElZ/QNPPG1hcyGKV8SoUSTVcyVmIA== X-Received: by 2002:a05:6a00:22cc:: with SMTP id f12mr4384475pfj.42.1598282801603; Mon, 24 Aug 2020 08:26:41 -0700 (PDT) Received: from localhost (199-83-220-90.PUBLIC.monkeybrains.net. [199.83.220.90]) by smtp.gmail.com with ESMTPSA id q17sm11709647pfh.32.2020.08.24.08.26.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Aug 2020 08:26:40 -0700 (PDT) From: Jack Kamm To: emacs-orgmode@gnu.org Subject: [RFC] ob-reticulate: R+Python interface from Babel Date: Mon, 24 Aug 2020 08:26:40 -0700 Message-ID: <875z98gj4f.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::434; envelope-from=jackkamm@gmail.com; helo=mail-pf1-x434.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=qKPGTyXc; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Spam-Score: -1.71 X-TUID: Ec7saiJKPM1a --=-=-= Content-Type: text/plain 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 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-ob-reticulate-Babel-source-lang-for-R-Python-reticul.patch >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 --=-=-=--