2015-10-25 20:56:00 -04:00
|
|
|
;;; ob-R.el --- Babel Functions for R -*- lexical-binding: t; -*-
|
2009-03-25 17:42:57 -04:00
|
|
|
|
2024-01-01 20:47:10 -05:00
|
|
|
;; Copyright (C) 2009-2024 Free Software Foundation, Inc.
|
2009-03-25 17:42:57 -04:00
|
|
|
|
2011-08-24 14:55:11 -04:00
|
|
|
;; Author: Eric Schulte
|
|
|
|
;; Dan Davison
|
2021-10-03 02:54:33 -04:00
|
|
|
;; Maintainer: Jeremie Juste <jeremiejuste@gmail.com>
|
2009-03-29 18:09:06 -04:00
|
|
|
;; Keywords: literate programming, reproducible research, R, statistics
|
2021-09-26 03:44:29 -04:00
|
|
|
;; URL: https://orgmode.org
|
2009-03-25 17:42:57 -04:00
|
|
|
|
2010-06-25 12:32:35 -04:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-03-25 17:42:57 -04:00
|
|
|
|
2010-06-25 12:32:35 -04:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-03-25 17:42:57 -04:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 12:32:35 -04:00
|
|
|
;; 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,
|
2009-03-25 17:42:57 -04:00
|
|
|
;; 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.
|
2010-06-25 12:32:35 -04:00
|
|
|
|
2009-03-25 17:42:57 -04:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 18:52:52 -04:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2009-03-25 17:42:57 -04:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
;; Org-Babel support for evaluating R code
|
2009-03-25 17:42:57 -04:00
|
|
|
|
|
|
|
;;; Code:
|
2016-07-25 10:24:14 -04:00
|
|
|
|
2022-08-04 09:53:05 -04:00
|
|
|
(require 'org-macs)
|
|
|
|
(org-assert-version)
|
|
|
|
|
2015-11-06 07:04:20 -05:00
|
|
|
(require 'cl-lib)
|
2016-07-25 10:24:14 -04:00
|
|
|
(require 'ob)
|
2010-07-02 22:21:31 -04:00
|
|
|
|
2010-08-27 16:35:09 -04:00
|
|
|
(declare-function orgtbl-to-tsv "org-table" (table params))
|
2023-04-17 14:17:55 -04:00
|
|
|
(declare-function run-ess-r "ext:ess-r-mode" (&optional start-args))
|
2010-07-04 11:24:08 -04:00
|
|
|
(declare-function inferior-ess-send-input "ext:ess-inf" ())
|
2010-08-27 16:42:24 -04:00
|
|
|
(declare-function ess-make-buffer-current "ext:ess-inf" ())
|
2010-08-30 21:20:01 -04:00
|
|
|
(declare-function ess-eval-buffer "ext:ess-inf" (vis))
|
2014-09-16 22:57:59 -04:00
|
|
|
(declare-function ess-wait-for-process "ext:ess-inf"
|
2016-05-15 17:42:47 -04:00
|
|
|
(&optional proc sec-prompt wait force-redisplay))
|
2009-03-25 17:42:57 -04:00
|
|
|
|
2012-04-10 19:03:37 -04:00
|
|
|
(defconst org-babel-header-args:R
|
|
|
|
'((width . :any)
|
|
|
|
(height . :any)
|
|
|
|
(bg . :any)
|
|
|
|
(units . :any)
|
|
|
|
(pointsize . :any)
|
|
|
|
(antialias . :any)
|
|
|
|
(quality . :any)
|
|
|
|
(compression . :any)
|
|
|
|
(res . :any)
|
|
|
|
(type . :any)
|
|
|
|
(family . :any)
|
|
|
|
(title . :any)
|
|
|
|
(fonts . :any)
|
|
|
|
(version . :any)
|
|
|
|
(paper . :any)
|
|
|
|
(encoding . :any)
|
|
|
|
(pagecentre . :any)
|
|
|
|
(colormodel . :any)
|
|
|
|
(useDingbats . :any)
|
|
|
|
(horizontal . :any)
|
|
|
|
(results . ((file list vector table scalar verbatim)
|
2015-05-19 17:22:11 -04:00
|
|
|
(raw html latex org code pp drawer)
|
|
|
|
(replace silent none append prepend)
|
2012-04-10 19:03:37 -04:00
|
|
|
(output value graphics))))
|
2010-05-28 09:52:00 -04:00
|
|
|
"R-specific header arguments.")
|
|
|
|
|
2013-10-28 15:40:32 -04:00
|
|
|
(defconst ob-R-safe-header-args
|
|
|
|
(append org-babel-safe-header-args
|
|
|
|
'(:width :height :bg :units :pointsize :antialias :quality
|
|
|
|
:compression :res :type :family :title :fonts
|
|
|
|
:version :paper :encoding :pagecentre :colormodel
|
|
|
|
:useDingbats :horizontal))
|
|
|
|
"Header args which are safe for R babel blocks.
|
|
|
|
|
|
|
|
See `org-babel-safe-header-args' for documentation of the format of
|
|
|
|
this variable.")
|
|
|
|
|
2010-06-25 13:12:54 -04:00
|
|
|
(defvar org-babel-default-header-args:R '())
|
2013-10-28 15:40:32 -04:00
|
|
|
(put 'org-babel-default-header-args:R 'safe-local-variable
|
|
|
|
(org-babel-header-args-safe-fn ob-R-safe-header-args))
|
2010-06-25 13:12:54 -04:00
|
|
|
|
2012-06-30 09:13:08 -04:00
|
|
|
(defcustom org-babel-R-command "R --slave --no-save"
|
|
|
|
"Name of command to use for executing R code."
|
|
|
|
:group 'org-babel
|
|
|
|
:version "24.1"
|
|
|
|
:type 'string)
|
2010-07-04 16:31:34 -04:00
|
|
|
|
2014-10-06 07:48:49 -04:00
|
|
|
;; The usage of utils::read.table() ensures that the command
|
|
|
|
;; read.table() can be found even in circumstances when the utils
|
|
|
|
;; package is not in the search path from R.
|
2014-06-23 06:11:59 -04:00
|
|
|
(defconst ob-R-transfer-variable-table-with-header
|
|
|
|
"%s <- local({
|
|
|
|
con <- textConnection(
|
|
|
|
%S
|
|
|
|
)
|
2014-10-06 07:48:49 -04:00
|
|
|
res <- utils::read.table(
|
2014-06-23 06:11:59 -04:00
|
|
|
con,
|
|
|
|
header = %s,
|
|
|
|
row.names = %s,
|
|
|
|
sep = \"\\t\",
|
|
|
|
as.is = TRUE
|
|
|
|
)
|
|
|
|
close(con)
|
|
|
|
res
|
|
|
|
})"
|
2014-07-27 18:01:23 -04:00
|
|
|
"R code used to transfer a table defined as a variable from org to R.
|
2014-10-06 07:48:49 -04:00
|
|
|
|
2014-06-23 06:11:59 -04:00
|
|
|
This function is used when the table contains a header.")
|
|
|
|
|
|
|
|
(defconst ob-R-transfer-variable-table-without-header
|
|
|
|
"%s <- local({
|
|
|
|
con <- textConnection(
|
|
|
|
%S
|
|
|
|
)
|
2014-10-06 07:48:49 -04:00
|
|
|
res <- utils::read.table(
|
2014-06-23 06:11:59 -04:00
|
|
|
con,
|
|
|
|
header = %s,
|
|
|
|
row.names = %s,
|
|
|
|
sep = \"\\t\",
|
|
|
|
as.is = TRUE,
|
|
|
|
fill = TRUE,
|
|
|
|
col.names = paste(\"V\", seq_len(%d), sep =\"\")
|
|
|
|
)
|
|
|
|
close(con)
|
|
|
|
res
|
|
|
|
})"
|
2014-07-27 18:01:23 -04:00
|
|
|
"R code used to transfer a table defined as a variable from org to R.
|
2014-10-06 07:48:49 -04:00
|
|
|
|
2014-06-23 06:11:59 -04:00
|
|
|
This function is used when the table does not contain a header.")
|
|
|
|
|
2016-02-02 12:02:08 -05:00
|
|
|
(defun org-babel-expand-body:R (body params &optional _graphics-file)
|
2010-06-11 20:12:15 -04:00
|
|
|
"Expand BODY according to PARAMS, return the expanded body."
|
2014-05-20 22:22:30 -04:00
|
|
|
(mapconcat 'identity
|
2014-04-22 15:13:48 -04:00
|
|
|
(append
|
2016-09-22 13:45:15 -04:00
|
|
|
(when (cdr (assq :prologue params))
|
|
|
|
(list (cdr (assq :prologue params))))
|
2014-04-22 15:13:48 -04:00
|
|
|
(org-babel-variable-assignments:R params)
|
|
|
|
(list body)
|
2016-09-22 13:45:15 -04:00
|
|
|
(when (cdr (assq :epilogue params))
|
|
|
|
(list (cdr (assq :epilogue params)))))
|
2014-04-22 15:13:48 -04:00
|
|
|
"\n"))
|
2010-04-20 12:16:21 -04:00
|
|
|
|
2009-07-08 00:13:07 -04:00
|
|
|
(defun org-babel-execute:R (body params)
|
2023-08-10 06:58:14 -04:00
|
|
|
"Execute a block of R code BODY according to PARAMS.
|
2010-07-13 18:56:17 -04:00
|
|
|
This function is called by `org-babel-execute-src-block'."
|
2010-02-22 13:37:16 -05:00
|
|
|
(save-excursion
|
2016-09-22 13:45:15 -04:00
|
|
|
(let* ((result-params (cdr (assq :result-params params)))
|
|
|
|
(result-type (cdr (assq :result-type params)))
|
2021-09-26 12:25:23 -04:00
|
|
|
(async (org-babel-comint-use-async params))
|
2010-07-04 16:31:34 -04:00
|
|
|
(session (org-babel-R-initiate-session
|
2016-09-22 13:45:15 -04:00
|
|
|
(cdr (assq :session params)) params))
|
2014-04-22 15:13:48 -04:00
|
|
|
(graphics-file (and (member "graphics" (assq :result-params params))
|
|
|
|
(org-babel-graphical-output-file params)))
|
2016-12-28 21:32:07 -05:00
|
|
|
(colnames-p (unless graphics-file (cdr (assq :colnames params))))
|
|
|
|
(rownames-p (unless graphics-file (cdr (assq :rownames params))))
|
2014-03-23 22:01:37 -04:00
|
|
|
(full-body
|
|
|
|
(let ((inside
|
|
|
|
(list (org-babel-expand-body:R body params graphics-file))))
|
2014-05-20 22:22:30 -04:00
|
|
|
(mapconcat 'identity
|
2014-03-23 22:01:37 -04:00
|
|
|
(if graphics-file
|
|
|
|
(append
|
|
|
|
(list (org-babel-R-construct-graphics-device-call
|
|
|
|
graphics-file params))
|
|
|
|
inside
|
|
|
|
(list "},error=function(e){plot(x=-1:1, y=-1:1, type='n', xlab='', ylab='', axes=FALSE); text(x=0, y=0, labels=e$message, col='red'); paste('ERROR', e$message, sep=' : ')}); dev.off()"))
|
|
|
|
inside)
|
|
|
|
"\n")))
|
2010-04-30 14:14:13 -04:00
|
|
|
(result
|
|
|
|
(org-babel-R-evaluate
|
2011-09-15 17:43:35 -04:00
|
|
|
session full-body result-type result-params
|
2010-04-30 14:14:13 -04:00
|
|
|
(or (equal "yes" colnames-p)
|
2010-10-21 07:52:24 -04:00
|
|
|
(org-babel-pick-name
|
2016-09-22 13:45:15 -04:00
|
|
|
(cdr (assq :colname-names params)) colnames-p))
|
2010-04-30 14:14:13 -04:00
|
|
|
(or (equal "yes" rownames-p)
|
2010-10-21 07:52:24 -04:00
|
|
|
(org-babel-pick-name
|
2021-09-26 12:25:23 -04:00
|
|
|
(cdr (assq :rowname-names params)) rownames-p))
|
|
|
|
async)))
|
2010-12-20 12:35:23 -05:00
|
|
|
(if graphics-file nil result))))
|
2009-04-04 19:23:35 -04:00
|
|
|
|
2009-06-14 14:48:11 -04:00
|
|
|
(defun org-babel-prep-session:R (session params)
|
|
|
|
"Prepare SESSION according to the header arguments specified in PARAMS."
|
2010-03-13 12:59:41 -05:00
|
|
|
(let* ((session (org-babel-R-initiate-session session params))
|
2010-10-21 06:34:55 -04:00
|
|
|
(var-lines (org-babel-variable-assignments:R params)))
|
2010-04-08 15:26:08 -04:00
|
|
|
(org-babel-comint-in-buffer session
|
|
|
|
(mapc (lambda (var)
|
Xemacs incompatibilities
Carsten Dominik <carsten.dominik@gmail.com> writes:
> On May 17, 2010, at 4:39 PM, Michael Sperber wrote:
>
>> In particular, fixing the require won't be enough: org-babel-python.el
>> uses `run-python' and interacts with the inferior Python, whereas
>> python-mode.el defines `py-shell'.
>>
>> Should I try to abstract over the differences?
>
> Yes, this would be much appreciated. (I think, Eric or Dan?)
OK, I've attached a patch that makes `org-babel-python' work on XEmacs.
Most of the issues are pure XEmacs issues. Notes:
- XEmacs doesn't have [[:digit:]] - I hope to rectify this in the
future, but it seems there's no downside in this particular case to
replacing by [0-9].
- XEmacs doesn't have `move-end-of-line', but does have `end-of-line'.
I don't understand the intent of having both of these, but the code
seems fine with `end-of-line'.
- It seems there are way too few `require's throughout org-babel. I
don't know if it's OK to add the ones I needed.
- `org-babel-python-evaluate' looked broken as-is: It doesn't use the
`body' argument properly, the result is (I think) processed in the
wrong order and not properly split into lines. I've fixed all these,
but a review is probably in order.
2010-05-24 15:22:50 -04:00
|
|
|
(end-of-line 1) (insert var) (comint-send-input nil t)
|
2020-02-18 16:57:37 -05:00
|
|
|
(org-babel-comint-wait-for-output session))
|
|
|
|
var-lines))
|
2010-01-11 12:14:30 -05:00
|
|
|
session))
|
|
|
|
|
|
|
|
(defun org-babel-load-session:R (session body params)
|
|
|
|
"Load BODY into SESSION."
|
|
|
|
(save-window-excursion
|
|
|
|
(let ((buffer (org-babel-prep-session:R session params)))
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(goto-char (process-mark (get-buffer-process (current-buffer))))
|
|
|
|
(insert (org-babel-chomp body)))
|
|
|
|
buffer)))
|
2009-06-14 14:48:11 -04:00
|
|
|
|
|
|
|
;; helper functions
|
|
|
|
|
2010-10-21 07:52:24 -04:00
|
|
|
(defun org-babel-variable-assignments:R (params)
|
2023-08-10 06:58:14 -04:00
|
|
|
"Return list of R statements assigning the block's variables.
|
|
|
|
Retrieve variables from PARAMS."
|
2015-10-29 15:26:11 -04:00
|
|
|
(let ((vars (org-babel--get-vars params)))
|
2010-10-14 16:08:21 -04:00
|
|
|
(mapcar
|
|
|
|
(lambda (pair)
|
|
|
|
(org-babel-R-assign-elisp
|
|
|
|
(car pair) (cdr pair)
|
2016-09-22 13:45:15 -04:00
|
|
|
(equal "yes" (cdr (assq :colnames params)))
|
|
|
|
(equal "yes" (cdr (assq :rownames params)))))
|
2010-10-14 16:08:21 -04:00
|
|
|
(mapcar
|
|
|
|
(lambda (i)
|
2010-10-21 07:52:24 -04:00
|
|
|
(cons (car (nth i vars))
|
2010-10-14 16:08:21 -04:00
|
|
|
(org-babel-reassemble-table
|
2010-10-21 07:52:24 -04:00
|
|
|
(cdr (nth i vars))
|
2016-09-22 13:45:15 -04:00
|
|
|
(cdr (nth i (cdr (assq :colname-names params))))
|
|
|
|
(cdr (nth i (cdr (assq :rowname-names params)))))))
|
2016-06-23 04:00:00 -04:00
|
|
|
(number-sequence 0 (1- (length vars)))))))
|
2010-10-14 16:08:21 -04:00
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defun org-babel-R-quote-tsv-field (s)
|
2009-04-04 19:23:35 -04:00
|
|
|
"Quote field S for export to R."
|
2009-05-22 10:57:35 -04:00
|
|
|
(if (stringp s)
|
|
|
|
(concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
|
|
|
|
(format "%S" s)))
|
2009-04-04 19:23:35 -04:00
|
|
|
|
2010-04-19 17:25:43 -04:00
|
|
|
(defun org-babel-R-assign-elisp (name value colnames-p rownames-p)
|
2010-04-08 15:26:08 -04:00
|
|
|
"Construct R code assigning the elisp VALUE to a variable named NAME."
|
2009-06-15 15:17:58 -04:00
|
|
|
(if (listp value)
|
2022-12-07 15:25:19 -05:00
|
|
|
(let* ((lengths (mapcar 'length (cl-remove-if-not 'listp value)))
|
2014-05-20 22:21:00 -04:00
|
|
|
(max (if lengths (apply 'max lengths) 0))
|
2014-06-20 16:19:59 -04:00
|
|
|
(min (if lengths (apply 'min lengths) 0)))
|
2014-05-20 22:21:00 -04:00
|
|
|
;; Ensure VALUE has an orgtbl structure (depth of at least 2).
|
2022-12-07 15:25:19 -05:00
|
|
|
(unless (listp (car value)) (setq value (mapcar 'list value)))
|
2014-06-20 16:19:59 -04:00
|
|
|
(let ((file (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
|
2012-04-25 15:34:15 -04:00
|
|
|
(header (if (or (eq (nth 1 value) 'hline) colnames-p)
|
|
|
|
"TRUE" "FALSE"))
|
|
|
|
(row-names (if rownames-p "1" "NULL")))
|
|
|
|
(if (= max min)
|
2014-06-23 06:11:59 -04:00
|
|
|
(format ob-R-transfer-variable-table-with-header
|
|
|
|
name file header row-names)
|
|
|
|
(format ob-R-transfer-variable-table-without-header
|
|
|
|
name file header row-names max))))
|
|
|
|
(cond ((integerp value) (format "%s <- %s" name (concat (number-to-string value) "L")))
|
|
|
|
((floatp value) (format "%s <- %s" name value))
|
2014-08-07 01:46:11 -04:00
|
|
|
((stringp value) (format "%s <- %S" name (org-no-properties value)))
|
2014-06-23 06:11:59 -04:00
|
|
|
(t (format "%s <- %S" name (prin1-to-string value))))))
|
|
|
|
|
2009-05-17 20:42:04 -04:00
|
|
|
|
2024-01-09 13:08:44 -05:00
|
|
|
(defvar ess-current-process-name) ; dynamically scoped
|
|
|
|
(defvar ess-local-process-name) ; dynamically scoped
|
2012-09-23 05:07:18 -04:00
|
|
|
(defvar ess-ask-for-ess-directory) ; dynamically scoped
|
2024-01-05 08:56:21 -05:00
|
|
|
(defvar ess-gen-proc-buffer-name-function) ; defined in ess-inf.el
|
2010-03-13 12:59:41 -05:00
|
|
|
(defun org-babel-R-initiate-session (session params)
|
2023-08-10 06:58:14 -04:00
|
|
|
"Create or return the current R SESSION buffer.
|
|
|
|
Use PARAMS to set default directory when creating a new session."
|
2009-06-15 14:49:06 -04:00
|
|
|
(unless (string= session "none")
|
2024-01-05 08:56:21 -05:00
|
|
|
(let* ((session (or session "*R*"))
|
|
|
|
(ess-ask-for-ess-directory
|
|
|
|
(and (boundp 'ess-ask-for-ess-directory)
|
|
|
|
ess-ask-for-ess-directory
|
|
|
|
(not (cdr (assq :dir params)))))
|
|
|
|
;; Make ESS name the process buffer as SESSION.
|
|
|
|
(ess-gen-proc-buffer-name-function
|
|
|
|
(lambda (_) session)))
|
2010-03-13 12:59:41 -05:00
|
|
|
(if (org-babel-comint-buffer-livep session)
|
|
|
|
session
|
|
|
|
(save-window-excursion
|
2013-04-03 09:46:04 -04:00
|
|
|
(when (get-buffer session)
|
|
|
|
;; Session buffer exists, but with dead process
|
|
|
|
(set-buffer session))
|
2023-04-17 14:17:55 -04:00
|
|
|
(org-require-package 'ess-r-mode "ESS")
|
|
|
|
(set-buffer (run-ess-r))
|
2014-10-10 00:35:41 -04:00
|
|
|
(let ((R-proc (get-process (or ess-local-process-name
|
|
|
|
ess-current-process-name))))
|
|
|
|
(while (process-get R-proc 'callbacks)
|
|
|
|
(ess-wait-for-process R-proc)))
|
2013-04-03 09:46:04 -04:00
|
|
|
(current-buffer))))))
|
2009-06-11 20:04:42 -04:00
|
|
|
|
2010-08-17 16:44:28 -04:00
|
|
|
(defun org-babel-R-associate-session (session)
|
|
|
|
"Associate R code buffer with an R session.
|
|
|
|
Make SESSION be the inferior ESS process associated with the
|
|
|
|
current code buffer."
|
2024-01-09 13:08:44 -05:00
|
|
|
(when-let ((process (get-buffer-process session)))
|
|
|
|
(setq ess-local-process-name (process-name process))
|
|
|
|
(ess-make-buffer-current))
|
|
|
|
(setq-local ess-gen-proc-buffer-name-function (lambda (_) session)))
|
2010-08-17 16:44:28 -04:00
|
|
|
|
2013-04-21 23:28:26 -04:00
|
|
|
(defvar org-babel-R-graphics-devices
|
|
|
|
'((:bmp "bmp" "filename")
|
|
|
|
(:jpg "jpeg" "filename")
|
|
|
|
(:jpeg "jpeg" "filename")
|
|
|
|
(:tikz "tikz" "file")
|
|
|
|
(:tiff "tiff" "filename")
|
|
|
|
(:png "png" "filename")
|
|
|
|
(:svg "svg" "file")
|
|
|
|
(:pdf "pdf" "file")
|
|
|
|
(:ps "postscript" "file")
|
|
|
|
(:postscript "postscript" "file"))
|
|
|
|
"An alist mapping graphics file types to R functions.
|
|
|
|
|
|
|
|
Each member of this list is a list with three members:
|
|
|
|
1. the file extension of the graphics file, as an elisp :keyword
|
|
|
|
2. the R graphics device function to call to generate such a file
|
|
|
|
3. the name of the argument to this function which specifies the
|
|
|
|
file to write to (typically \"file\" or \"filename\")")
|
|
|
|
|
org-babel: capture graphical output from R
If a [:file filename.ext] header arg is provided, then all graphical
output from the source block is captured on disk, and output of the
source block is a link to the resulting file, as with the
graphics-only languages such as gnuplot, ditaa, dot, asymptote. An
attempt is made to find a graphics device corresponding to the file
extension (currently .png, .jpg, .jpeg, .tiff, .bmp, .pdf, .ps,
.postscript are recognised); if that fails, png format output is
created.
Additionally, values for several arguments to the R graphics
device can be passed using header args:
:width :height :bg :units :pointsize
:antialias :quality :compression :res :type
:family :title :fonts :version :paper :encoding
:pagecentre :colormodel :useDingbats :horizontal
Arguments to the R graphics device that are not supported as header
args can be passed as a string in R argument syntax, using the header
arg :R-dev-args
An example block is (although both bg and fg can be passed directly as
header args)
\#+begin_src R :file z.pdf :width 8 :height 8 :R-dev-args bg="olivedrab", fg="hotpink"
plot(matrix(rnorm(100), ncol=2), type="l")
\#+end_src
2009-10-19 21:00:24 -04:00
|
|
|
(defun org-babel-R-construct-graphics-device-call (out-file params)
|
2010-06-11 20:12:15 -04:00
|
|
|
"Construct the call to the graphics device."
|
2013-04-21 23:28:26 -04:00
|
|
|
(let* ((allowed-args '(:width :height :bg :units :pointsize
|
|
|
|
:antialias :quality :compression :res
|
|
|
|
:type :family :title :fonts :version
|
|
|
|
:paper :encoding :pagecentre :colormodel
|
|
|
|
:useDingbats :horizontal))
|
2016-09-01 13:44:30 -04:00
|
|
|
(device (file-name-extension out-file))
|
2013-04-21 23:28:26 -04:00
|
|
|
(device-info (or (assq (intern (concat ":" device))
|
|
|
|
org-babel-R-graphics-devices)
|
|
|
|
(assq :png org-babel-R-graphics-devices)))
|
2021-09-29 03:22:47 -04:00
|
|
|
(extra-args (cdr (assq :R-dev-args params))) filearg args)
|
2013-04-21 23:28:26 -04:00
|
|
|
(setq device (nth 1 device-info))
|
|
|
|
(setq filearg (nth 2 device-info))
|
2012-06-19 17:31:54 -04:00
|
|
|
(setq args (mapconcat
|
|
|
|
(lambda (pair)
|
|
|
|
(if (member (car pair) allowed-args)
|
|
|
|
(format ",%s=%S"
|
|
|
|
(substring (symbol-name (car pair)) 1)
|
|
|
|
(cdr pair)) ""))
|
|
|
|
params ""))
|
2014-01-21 04:35:52 -05:00
|
|
|
(format "%s(%s=\"%s\"%s%s%s); tryCatch({"
|
2012-06-19 17:31:54 -04:00
|
|
|
device filearg out-file args
|
2010-07-04 16:31:34 -04:00
|
|
|
(if extra-args "," "") (or extra-args ""))))
|
org-babel: capture graphical output from R
If a [:file filename.ext] header arg is provided, then all graphical
output from the source block is captured on disk, and output of the
source block is a link to the resulting file, as with the
graphics-only languages such as gnuplot, ditaa, dot, asymptote. An
attempt is made to find a graphics device corresponding to the file
extension (currently .png, .jpg, .jpeg, .tiff, .bmp, .pdf, .ps,
.postscript are recognised); if that fails, png format output is
created.
Additionally, values for several arguments to the R graphics
device can be passed using header args:
:width :height :bg :units :pointsize
:antialias :quality :compression :res :type
:family :title :fonts :version :paper :encoding
:pagecentre :colormodel :useDingbats :horizontal
Arguments to the R graphics device that are not supported as header
args can be passed as a string in R argument syntax, using the header
arg :R-dev-args
An example block is (although both bg and fg can be passed directly as
header args)
\#+begin_src R :file z.pdf :width 8 :height 8 :R-dev-args bg="olivedrab", fg="hotpink"
plot(matrix(rnorm(100), ncol=2), type="l")
\#+end_src
2009-10-19 21:00:24 -04:00
|
|
|
|
2015-01-23 12:31:33 -05:00
|
|
|
(defconst org-babel-R-eoe-indicator "'org_babel_R_eoe'")
|
|
|
|
(defconst org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
|
|
|
|
|
|
|
|
(defconst org-babel-R-write-object-command "{
|
|
|
|
function(object,transfer.file) {
|
|
|
|
object
|
|
|
|
invisible(
|
|
|
|
if (
|
|
|
|
inherits(
|
|
|
|
try(
|
|
|
|
{
|
|
|
|
tfile<-tempfile()
|
|
|
|
write.table(object, file=tfile, sep=\"\\t\",
|
2021-02-06 14:50:00 -05:00
|
|
|
na=\"\",row.names=%s,col.names=%s,
|
2015-01-23 12:31:33 -05:00
|
|
|
quote=FALSE)
|
|
|
|
file.rename(tfile,transfer.file)
|
|
|
|
},
|
|
|
|
silent=TRUE),
|
|
|
|
\"try-error\"))
|
|
|
|
{
|
|
|
|
if(!file.exists(transfer.file))
|
|
|
|
file.create(transfer.file)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}(object=%s,transfer.file=\"%s\")"
|
2020-12-19 11:35:50 -05:00
|
|
|
"Template for an R command to evaluate a block of code and write result to file.
|
2015-01-23 12:31:33 -05:00
|
|
|
|
|
|
|
Has four %s escapes to be filled in:
|
|
|
|
1. Row names, \"TRUE\" or \"FALSE\"
|
|
|
|
2. Column names, \"TRUE\" or \"FALSE\"
|
|
|
|
3. The code to be run (must be an expression, not a statement)
|
|
|
|
4. The name of the file to write to")
|
2009-06-11 20:04:42 -04:00
|
|
|
|
2010-07-04 16:31:34 -04:00
|
|
|
(defun org-babel-R-evaluate
|
2021-09-29 03:22:47 -04:00
|
|
|
(session body result-type result-params column-names-p row-names-p async)
|
2010-08-18 20:20:54 -04:00
|
|
|
"Evaluate R code in BODY."
|
2011-09-21 08:52:20 -04:00
|
|
|
(if session
|
2021-09-26 12:25:23 -04:00
|
|
|
(if async
|
|
|
|
(ob-session-async-org-babel-R-evaluate-session
|
2021-10-05 18:37:38 -04:00
|
|
|
session body result-type column-names-p row-names-p)
|
2021-09-29 03:22:47 -04:00
|
|
|
(org-babel-R-evaluate-session
|
|
|
|
session body result-type result-params column-names-p row-names-p))
|
2011-09-21 08:52:20 -04:00
|
|
|
(org-babel-R-evaluate-external-process
|
|
|
|
body result-type result-params column-names-p row-names-p)))
|
2010-08-18 20:20:54 -04:00
|
|
|
|
|
|
|
(defun org-babel-R-evaluate-external-process
|
2016-07-25 10:24:14 -04:00
|
|
|
(body result-type result-params column-names-p row-names-p)
|
2010-08-18 20:20:54 -04:00
|
|
|
"Evaluate BODY in external R process.
|
2015-09-21 00:24:12 -04:00
|
|
|
If RESULT-TYPE equals `output' then return standard output as a
|
|
|
|
string. If RESULT-TYPE equals `value' then return the value of the
|
2010-08-18 20:20:54 -04:00
|
|
|
last statement in BODY, as elisp."
|
2016-07-25 10:24:14 -04:00
|
|
|
(cl-case result-type
|
2010-08-18 20:20:54 -04:00
|
|
|
(value
|
2010-08-30 12:34:05 -04:00
|
|
|
(let ((tmp-file (org-babel-temp-file "R-")))
|
2010-08-18 20:20:54 -04:00
|
|
|
(org-babel-eval org-babel-R-command
|
2010-08-28 08:52:37 -04:00
|
|
|
(format org-babel-R-write-object-command
|
2010-08-18 20:20:54 -04:00
|
|
|
(if row-names-p "TRUE" "FALSE")
|
|
|
|
(if column-names-p
|
|
|
|
(if row-names-p "NA" "TRUE")
|
2010-08-28 08:52:37 -04:00
|
|
|
"FALSE")
|
|
|
|
(format "{function ()\n{\n%s\n}}()" body)
|
2010-09-22 16:40:14 -04:00
|
|
|
(org-babel-process-file-name tmp-file 'noquote)))
|
2010-08-18 20:20:54 -04:00
|
|
|
(org-babel-R-process-value-result
|
2012-11-18 20:02:09 -05:00
|
|
|
(org-babel-result-cond result-params
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert-file-contents tmp-file)
|
2016-01-19 22:32:11 -05:00
|
|
|
(org-babel-chomp (buffer-string) "\n"))
|
2011-09-15 17:43:35 -04:00
|
|
|
(org-babel-import-elisp-from-file tmp-file '(16)))
|
|
|
|
column-names-p)))
|
2010-08-18 20:20:54 -04:00
|
|
|
(output (org-babel-eval org-babel-R-command body))))
|
|
|
|
|
Backport changes from Emacs revs 115081 and 115082
2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca>
Address some byte-compiler warnings.
* ob-abc.el (org-babel-expand-body:abc): Use dolist.
(org-babel-execute:abc): Fix regexp quoting.
* ob-calc.el (org--var-syms): Rename from `var-syms'.
* ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding.
* ob-table.el (sbe): Move debug declaration.
* org-clock.el (org--msg-extra): Rename from `msg-extra'.
* org.el (org-version): Avoid var name starting with _.
(org-inhibit-startup, org-called-with-limited-levels)
(org-link-search-inhibit-query, org-time-was-given)
(org-end-time-was-given, org-def, org-defdecode, org-with-time):
* org-colview.el (org-agenda-overriding-columns-format):
* org-agenda.el (org-agenda-multi, org-depend-tag-blocked)
(org-agenda-show-log-scoped):
* ob-python.el (py-which-bufname, python-shell-buffer-name):
* ob-haskell.el (org-export-copy-to-kill-ring):
* ob-exp.el (org-link-search-inhibit-query):
* ob-R.el (ess-eval-visibly-p):
* ob-core.el (org-src-window-setup): Declare before use.
(org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'.
* ox-odt.el (org-odt-hfy-face-to-css):
* org-src.el (org-src-associate-babel-session, org-src-get-lang-mode):
* org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex)
(org-bibtex-check):
* ob-tangle.el (org-babel-tangle, org-babel-spec-to-string)
(org-babel-tangle-single-block, org-babel-tangle-comment-links):
* ob-table.el (sbe):
* ob-sqlite.el (org-babel-sqlite-expand-vars):
* ob-sql.el (org-babel-sql-expand-vars):
* ob-shen.el (org-babel-execute:shen):
* ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate):
* ob-scala.el (org-babel-scala-evaluate):
* ob-ruby.el (org-babel-ruby-table-or-string)
(org-babel-ruby-evaluate):
* ob-python.el (org-babel-python-table-or-string)
(org-babel-python-evaluate-external-process)
(org-babel-python-evaluate-session):
* ob-picolisp.el (org-babel-execute:picolisp):
* ob-perl.el (org-babel-perl-evaluate):
* ob-maxima.el (org-babel-execute:maxima):
* ob-lisp.el (org-babel-execute:lisp):
* ob-java.el (org-babel-execute:java):
* ob-io.el (org-babel-io-evaluate):
* ob-haskell.el (org-babel-execute:haskell):
* ob-fortran.el (org-babel-execute:fortran):
* ob-exp.el (org-babel-exp-code):
* ob-emacs-lisp.el (org-babel-execute:emacs-lisp):
* ob-ditaa.el (org-babel-execute:ditaa):
* ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash)
(org-babel-parse-header-arguments, org-babel-reassemble-table)
(org-babel-goto-src-block-head, org-babel-mark-block)
(org-babel-expand-noweb-references, org-babel-script-escape)
(org-babel-process-file-name):
* ob-clojure.el (org-babel-execute:clojure):
* ob-calc.el (org-babel-execute:calc):
* ob-awk.el (org-babel-execute:awk):
* ob-abc.el (org-babel-execute:abc):
* ob-R.el (org-babel-expand-body:R):
* ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...).
2013-11-12 Glenn Morris <rgm@gnu.org>
* ox-html.el (org-html-scripts): Add 2013 to copyright years.
(org-html-infojs-template): Copyright holder to FSF.
2013-11-12 14:57:31 -05:00
|
|
|
(defvar ess-eval-visibly-p)
|
|
|
|
|
2010-08-18 20:20:54 -04:00
|
|
|
(defun org-babel-R-evaluate-session
|
2016-07-25 10:24:14 -04:00
|
|
|
(session body result-type result-params column-names-p row-names-p)
|
2010-08-18 20:20:54 -04:00
|
|
|
"Evaluate BODY in SESSION.
|
2015-09-21 00:24:12 -04:00
|
|
|
If RESULT-TYPE equals `output' then return standard output as a
|
|
|
|
string. If RESULT-TYPE equals `value' then return the value of the
|
2010-08-18 20:20:54 -04:00
|
|
|
last statement in BODY, as elisp."
|
2016-07-25 10:24:14 -04:00
|
|
|
(cl-case result-type
|
2010-08-18 20:20:54 -04:00
|
|
|
(value
|
2010-08-30 12:26:02 -04:00
|
|
|
(with-temp-buffer
|
|
|
|
(insert (org-babel-chomp body))
|
|
|
|
(let ((ess-local-process-name
|
2012-01-30 11:30:39 -05:00
|
|
|
(process-name (get-buffer-process session)))
|
|
|
|
(ess-eval-visibly-p nil))
|
2012-01-30 20:35:49 -05:00
|
|
|
(ess-eval-buffer nil)))
|
2010-08-30 12:26:02 -04:00
|
|
|
(let ((tmp-file (org-babel-temp-file "R-")))
|
|
|
|
(org-babel-comint-eval-invisibly-and-wait-for-file
|
2010-08-30 12:34:05 -04:00
|
|
|
session tmp-file
|
2010-08-28 08:52:37 -04:00
|
|
|
(format org-babel-R-write-object-command
|
|
|
|
(if row-names-p "TRUE" "FALSE")
|
|
|
|
(if column-names-p
|
|
|
|
(if row-names-p "NA" "TRUE")
|
|
|
|
"FALSE")
|
2010-09-22 16:40:14 -04:00
|
|
|
".Last.value" (org-babel-process-file-name tmp-file 'noquote)))
|
2010-08-18 20:20:54 -04:00
|
|
|
(org-babel-R-process-value-result
|
2012-11-18 20:02:09 -05:00
|
|
|
(org-babel-result-cond result-params
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert-file-contents tmp-file)
|
2016-01-19 22:32:11 -05:00
|
|
|
(org-babel-chomp (buffer-string) "\n"))
|
2011-09-15 17:43:35 -04:00
|
|
|
(org-babel-import-elisp-from-file tmp-file '(16)))
|
|
|
|
column-names-p)))
|
2010-08-18 20:20:54 -04:00
|
|
|
(output
|
2014-08-07 14:00:01 -04:00
|
|
|
(mapconcat
|
|
|
|
'org-babel-chomp
|
|
|
|
(butlast
|
|
|
|
(delq nil
|
|
|
|
(mapcar
|
|
|
|
(lambda (line) (when (> (length line) 0) line))
|
|
|
|
(mapcar
|
|
|
|
(lambda (line) ;; cleanup extra prompts left in output
|
|
|
|
(if (string-match
|
2015-11-07 16:23:36 -05:00
|
|
|
"^\\([>+.]\\([ ][>.+]\\)*[ ]\\)"
|
|
|
|
(car (split-string line "\n")))
|
2014-08-07 14:00:01 -04:00
|
|
|
(substring line (match-end 1))
|
|
|
|
line))
|
2021-05-02 18:15:09 -04:00
|
|
|
(with-current-buffer session
|
|
|
|
(let ((comint-prompt-regexp (concat "^" comint-prompt-regexp)))
|
|
|
|
(org-babel-comint-with-output (session org-babel-R-eoe-output)
|
|
|
|
(insert (mapconcat 'org-babel-chomp
|
|
|
|
(list body org-babel-R-eoe-indicator)
|
|
|
|
"\n"))
|
|
|
|
(inferior-ess-send-input)))))))) "\n"))))
|
2009-07-05 19:19:14 -04:00
|
|
|
|
2009-07-19 12:01:00 -04:00
|
|
|
(defun org-babel-R-process-value-result (result column-names-p)
|
2010-07-13 18:56:17 -04:00
|
|
|
"R-specific processing of return value.
|
|
|
|
Insert hline if column names in output have been requested."
|
2009-07-19 12:01:00 -04:00
|
|
|
(if column-names-p
|
2020-05-24 10:08:52 -04:00
|
|
|
(condition-case nil
|
|
|
|
(cons (car result) (cons 'hline (cdr result)))
|
|
|
|
(error "Could not parse R result"))
|
2009-07-19 12:01:00 -04:00
|
|
|
result))
|
2009-04-03 19:50:57 -04:00
|
|
|
|
2021-09-26 12:25:23 -04:00
|
|
|
|
|
|
|
;;; async evaluation
|
|
|
|
|
|
|
|
(defconst ob-session-async-R-indicator "'ob_comint_async_R_%s_%s'")
|
|
|
|
|
|
|
|
(defun ob-session-async-org-babel-R-evaluate-session
|
2021-10-05 18:37:38 -04:00
|
|
|
(session body result-type column-names-p row-names-p)
|
2021-09-26 12:25:23 -04:00
|
|
|
"Asynchronously evaluate BODY in SESSION.
|
|
|
|
Returns a placeholder string for insertion, to later be replaced
|
|
|
|
by `org-babel-comint-async-filter'."
|
|
|
|
(org-babel-comint-async-register
|
|
|
|
session (current-buffer)
|
|
|
|
"^\\(?:[>.+] \\)*\\[1\\] \"ob_comint_async_R_\\(.+?\\)_\\(.+\\)\"$"
|
|
|
|
'org-babel-chomp
|
|
|
|
'ob-session-async-R-value-callback)
|
|
|
|
(cl-case result-type
|
|
|
|
(value
|
|
|
|
(let ((tmp-file (org-babel-temp-file "R-")))
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert
|
|
|
|
(org-babel-chomp body))
|
|
|
|
(let ((ess-local-process-name
|
|
|
|
(process-name (get-buffer-process session))))
|
|
|
|
(ess-eval-buffer nil)))
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert
|
|
|
|
(mapconcat
|
|
|
|
'org-babel-chomp
|
|
|
|
(list (format org-babel-R-write-object-command
|
|
|
|
(if row-names-p "TRUE" "FALSE")
|
|
|
|
(if column-names-p
|
|
|
|
(if row-names-p "NA" "TRUE")
|
|
|
|
"FALSE")
|
|
|
|
".Last.value"
|
|
|
|
(org-babel-process-file-name tmp-file 'noquote))
|
|
|
|
(format ob-session-async-R-indicator
|
|
|
|
"file" tmp-file))
|
|
|
|
"\n"))
|
|
|
|
(let ((ess-local-process-name
|
|
|
|
(process-name (get-buffer-process session))))
|
|
|
|
(ess-eval-buffer nil)))
|
|
|
|
tmp-file))
|
|
|
|
(output
|
2023-03-22 14:20:47 -04:00
|
|
|
(let ((uuid (org-id-uuid))
|
2021-09-26 12:25:23 -04:00
|
|
|
(ess-local-process-name
|
2021-10-05 18:37:38 -04:00
|
|
|
(process-name (get-buffer-process session)))
|
|
|
|
(ess-eval-visibly-p nil))
|
2021-09-26 12:25:23 -04:00
|
|
|
(with-temp-buffer
|
|
|
|
(insert (format ob-session-async-R-indicator
|
2021-09-29 03:22:47 -04:00
|
|
|
"start" uuid))
|
2021-09-26 12:25:23 -04:00
|
|
|
(insert "\n")
|
|
|
|
(insert body)
|
|
|
|
(insert "\n")
|
|
|
|
(insert (format ob-session-async-R-indicator
|
2021-09-27 16:02:17 -04:00
|
|
|
"end" uuid))
|
2021-10-05 18:37:38 -04:00
|
|
|
(ess-eval-buffer nil ))
|
2021-09-26 12:25:23 -04:00
|
|
|
uuid))))
|
|
|
|
|
|
|
|
(defun ob-session-async-R-value-callback (params tmp-file)
|
|
|
|
"Callback for async value results.
|
2022-11-21 09:16:08 -05:00
|
|
|
Assigned locally to `org-babel-comint-async-file-callback' in R
|
2021-09-26 12:25:23 -04:00
|
|
|
comint buffers used for asynchronous Babel evaluation."
|
|
|
|
(let* ((graphics-file (and (member "graphics" (assq :result-params params))
|
|
|
|
(org-babel-graphical-output-file params)))
|
|
|
|
(colnames-p (unless graphics-file (cdr (assq :colnames params)))))
|
|
|
|
(org-babel-R-process-value-result
|
|
|
|
(org-babel-result-cond (assq :result-params params)
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert-file-contents tmp-file)
|
|
|
|
(org-babel-chomp (buffer-string) "\n"))
|
|
|
|
(org-babel-import-elisp-from-file tmp-file '(16)))
|
|
|
|
(or (equal "yes" colnames-p)
|
|
|
|
(org-babel-pick-name
|
|
|
|
(cdr (assq :colname-names params)) colnames-p)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; ob-session-async-R.el ends here
|
|
|
|
|
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
(provide 'ob-R)
|
2010-06-25 12:32:35 -04:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
;;; ob-R.el ends here
|