2010-06-11 19:02:42 -04:00
|
|
|
;;; ob-R.el --- org-babel functions for R code evaluation
|
2009-03-25 17:42:57 -04:00
|
|
|
|
2010-06-25 12:32:35 -04:00
|
|
|
;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
|
2009-03-25 17:42:57 -04:00
|
|
|
|
2010-06-25 12:32:35 -04:00
|
|
|
;; Author: Eric Schulte, Dan Davison
|
2009-03-29 18:09:06 -04:00
|
|
|
;; Keywords: literate programming, reproducible research, R, statistics
|
2009-03-25 17:42:57 -04:00
|
|
|
;; Homepage: http://orgmode.org
|
|
|
|
;; Version: 0.01
|
|
|
|
|
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
|
2010-06-25 12:32:35 -04:00
|
|
|
;; along with GNU Emacs. If not, see <http://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:
|
2010-06-11 19:02:42 -04:00
|
|
|
(require 'ob)
|
2010-07-02 22:21:31 -04:00
|
|
|
(require 'ob-ref)
|
2010-07-04 17:46:13 -04:00
|
|
|
(require 'ob-comint)
|
|
|
|
(require 'ob-eval)
|
2010-07-02 22:21:31 -04:00
|
|
|
(eval-when-compile (require 'cl))
|
|
|
|
|
|
|
|
(declare-function orgtbl-to-tsv "ob-table" (table params))
|
2010-07-04 11:24:08 -04:00
|
|
|
(declare-function R "ext:essd-r" (&optional start-args))
|
|
|
|
(declare-function inferior-ess-send-input "ext:ess-inf" ())
|
2009-03-25 17:42:57 -04:00
|
|
|
|
2010-05-28 09:52:00 -04:00
|
|
|
(defconst org-babel-header-arg-names:R
|
|
|
|
'(width height bg units pointsize antialias quality compression
|
|
|
|
res type family title fonts version paper encoding
|
|
|
|
pagecentre colormodel useDingbats horizontal)
|
|
|
|
"R-specific header arguments.")
|
|
|
|
|
2010-06-25 13:12:54 -04:00
|
|
|
(defvar org-babel-default-header-args:R '())
|
|
|
|
|
2010-07-04 16:31:34 -04:00
|
|
|
(defvar org-babel-R-command "R --slave --no-save"
|
|
|
|
"Name of command to use for executing R code.")
|
|
|
|
|
2010-04-20 12:16:21 -04:00
|
|
|
(defun org-babel-expand-body:R (body params &optional processed-params)
|
2010-06-11 20:12:15 -04:00
|
|
|
"Expand BODY according to PARAMS, return the expanded body."
|
2010-04-20 12:16:21 -04:00
|
|
|
(let* ((processed-params (or processed-params
|
|
|
|
(org-babel-process-params params)))
|
2010-07-04 16:31:34 -04:00
|
|
|
(vars (mapcar
|
|
|
|
(lambda (i)
|
|
|
|
(cons (car (nth i (nth 1 processed-params)))
|
|
|
|
(org-babel-reassemble-table
|
|
|
|
(cdr (nth i (nth 1 processed-params)))
|
|
|
|
(cdr (nth i (nth 4 processed-params)))
|
|
|
|
(cdr (nth i (nth 5 processed-params))))))
|
|
|
|
(number-sequence 0 (1- (length (nth 1 processed-params))))))
|
2010-04-20 12:16:21 -04:00
|
|
|
(out-file (cdr (assoc :file params))))
|
2010-07-04 16:31:34 -04:00
|
|
|
(mapconcat ;; define any variables
|
|
|
|
#'org-babel-trim
|
|
|
|
((lambda (inside)
|
|
|
|
(if out-file
|
|
|
|
(append
|
2010-07-05 13:10:11 -04:00
|
|
|
(list (org-babel-R-construct-graphics-device-call out-file params))
|
2010-07-04 16:31:34 -04:00
|
|
|
inside
|
|
|
|
(list "dev.off()"))
|
|
|
|
inside))
|
|
|
|
(append
|
|
|
|
(mapcar
|
|
|
|
(lambda (pair)
|
|
|
|
(org-babel-R-assign-elisp
|
|
|
|
(car pair) (cdr pair)
|
|
|
|
(equal "yes" (cdr (assoc :colnames params)))
|
|
|
|
(equal "yes" (cdr (assoc :rownames params)))))
|
|
|
|
vars)
|
|
|
|
(list body))) "\n")))
|
2010-04-20 12:16:21 -04:00
|
|
|
|
2009-07-08 00:13:07 -04:00
|
|
|
(defun org-babel-execute:R (body params)
|
2009-05-24 16:38:01 -04:00
|
|
|
"Execute a block of R code with org-babel. This function is
|
2009-11-10 15:42:05 -05:00
|
|
|
called by `org-babel-execute-src-block'."
|
2009-03-29 18:09:06 -04:00
|
|
|
(message "executing R source code block...")
|
2010-02-22 13:37:16 -05:00
|
|
|
(save-excursion
|
2009-11-10 17:22:25 -05:00
|
|
|
(let* ((processed-params (org-babel-process-params params))
|
2010-06-25 13:26:48 -04:00
|
|
|
(result-type (nth 3 processed-params))
|
2010-07-04 16:31:34 -04:00
|
|
|
(session (org-babel-R-initiate-session
|
|
|
|
(first processed-params) params))
|
2010-04-30 14:14:13 -04:00
|
|
|
(colnames-p (cdr (assoc :colnames params)))
|
|
|
|
(rownames-p (cdr (assoc :rownames params)))
|
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
|
|
|
(out-file (cdr (assoc :file params)))
|
2010-04-20 12:16:21 -04:00
|
|
|
(full-body (org-babel-expand-body:R body params processed-params))
|
2010-04-30 14:14:13 -04:00
|
|
|
(result
|
|
|
|
(org-babel-R-evaluate
|
|
|
|
session full-body result-type
|
|
|
|
(or (equal "yes" colnames-p)
|
|
|
|
(org-babel-pick-name (nth 4 processed-params) colnames-p))
|
|
|
|
(or (equal "yes" rownames-p)
|
|
|
|
(org-babel-pick-name (nth 5 processed-params) rownames-p)))))
|
2010-07-04 16:31:34 -04:00
|
|
|
(message "result is %S" result)
|
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
|
|
|
(or out-file 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-04-08 15:26:08 -04:00
|
|
|
(vars (org-babel-ref-variables params))
|
|
|
|
(var-lines
|
|
|
|
(mapcar
|
2010-04-19 17:25:43 -04:00
|
|
|
(lambda (pair) (org-babel-R-assign-elisp
|
2010-07-04 16:31:34 -04:00
|
|
|
(car pair) (cdr pair)
|
|
|
|
(equal (cdr (assoc :colnames params)) "yes")
|
|
|
|
(equal (cdr (assoc :rownames params)) "yes")))
|
2010-04-19 17:25:43 -04:00
|
|
|
vars)))
|
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)
|
2010-04-08 15:26:08 -04: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
|
|
|
|
|
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)
|
|
|
|
(let ((transition-file (make-temp-file "org-babel-R-import")))
|
|
|
|
;; ensure VALUE has an orgtbl structure (depth of at least 2)
|
|
|
|
(unless (listp (car value)) (setq value (list value)))
|
2010-02-25 11:03:57 -05:00
|
|
|
(with-temp-file (org-babel-maybe-remote-file transition-file)
|
2009-06-15 15:17:58 -04:00
|
|
|
(insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
|
|
|
|
(insert "\n"))
|
2010-04-19 17:25:43 -04:00
|
|
|
(format "%s <- read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE)"
|
|
|
|
name transition-file
|
2010-06-25 13:26:48 -04:00
|
|
|
(if (or (eq (nth 1 value) 'hline) colnames-p) "TRUE" "FALSE")
|
2010-04-19 17:25:43 -04:00
|
|
|
(if rownames-p "1" "NULL")))
|
2009-06-15 15:17:58 -04:00
|
|
|
(format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
|
2009-05-17 20:42:04 -04:00
|
|
|
|
2010-03-13 12:59:41 -05:00
|
|
|
(defun org-babel-R-initiate-session (session params)
|
2009-06-11 20:04:42 -04:00
|
|
|
"If there is not a current R process then create one."
|
2009-06-15 14:49:06 -04:00
|
|
|
(unless (string= session "none")
|
2010-03-13 12:59:41 -05:00
|
|
|
(let ((session (or session "*R*"))
|
|
|
|
(ess-ask-for-ess-directory (not (cdr (assoc :dir params)))))
|
|
|
|
(if (org-babel-comint-buffer-livep session)
|
|
|
|
session
|
|
|
|
(save-window-excursion
|
2010-07-04 16:31:34 -04:00
|
|
|
(require 'ess) (R)
|
|
|
|
(rename-buffer
|
|
|
|
(if (bufferp session)
|
|
|
|
(buffer-name session)
|
|
|
|
(if (stringp session)
|
|
|
|
session
|
|
|
|
(buffer-name))))
|
|
|
|
(current-buffer))))))
|
2009-06-11 20:04:42 -04:00
|
|
|
|
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."
|
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
|
|
|
(let ((devices
|
|
|
|
'((:bmp . "bmp")
|
|
|
|
(:jpg . "jpeg")
|
|
|
|
(:jpeg . "jpeg")
|
|
|
|
(:tiff . "tiff")
|
|
|
|
(:png . "png")
|
2010-04-01 18:45:59 -04:00
|
|
|
(:svg . "svg")
|
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
|
|
|
(:pdf . "pdf")
|
|
|
|
(:ps . "postscript")
|
|
|
|
(:postscript . "postscript")))
|
|
|
|
(allowed-args '(:width :height :bg :units :pointsize
|
2010-07-04 16:31:34 -04:00
|
|
|
:antialias :quality :compression :res
|
|
|
|
:type :family :title :fonts :version
|
|
|
|
:paper :encoding :pagecentre :colormodel
|
|
|
|
:useDingbats :horizontal))
|
|
|
|
(device (and (string-match ".+\\.\\([^.]+\\)" out-file)
|
|
|
|
(match-string 1 out-file)))
|
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
|
|
|
(extra-args (cdr (assq :R-dev-args params))) filearg args)
|
2010-07-04 16:31:34 -04:00
|
|
|
(setq device (or (and device (cdr (assq (intern (concat ":" device))
|
|
|
|
devices))) "png"))
|
|
|
|
(setq filearg
|
|
|
|
(if (member device '("pdf" "postscript" "svg")) "file" "filename"))
|
|
|
|
(setq args (mapconcat
|
|
|
|
(lambda (pair)
|
|
|
|
(if (member (car pair) allowed-args)
|
|
|
|
(format ",%s=%s"
|
|
|
|
(substring (symbol-name (car pair)) 1)
|
|
|
|
(cdr pair)) ""))
|
|
|
|
params ""))
|
|
|
|
(format "%s(%s=\"%s\"%s%s%s)"
|
|
|
|
device filearg out-file args
|
|
|
|
(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
|
|
|
|
2009-06-11 20:04:42 -04:00
|
|
|
(defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
|
|
|
|
(defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
|
2009-06-15 14:49:06 -04:00
|
|
|
(defvar org-babel-R-wrapper-method "main <- function ()\n{\n%s\n}
|
2010-04-09 20:32:37 -04:00
|
|
|
write.table(main(), file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=%s, col.names=%s, quote=FALSE)")
|
2010-07-04 16:31:34 -04:00
|
|
|
(defvar org-babel-R-wrapper-lastvar "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=%s, col.names=%s, quote=FALSE)")
|
2009-06-11 20:04:42 -04:00
|
|
|
|
2010-07-04 16:31:34 -04:00
|
|
|
(defun org-babel-R-evaluate
|
|
|
|
(session body result-type column-names-p row-names-p)
|
2009-11-10 17:22:25 -05:00
|
|
|
"Pass BODY to the R process in SESSION. If RESULT-TYPE equals
|
2009-06-11 20:04:42 -04:00
|
|
|
'output then return a list of the outputs of the statements in
|
|
|
|
BODY, if RESULT-TYPE equals 'value then return the value of the
|
2009-07-05 21:23:17 -04:00
|
|
|
last statement in BODY, as elisp."
|
2009-06-15 14:49:06 -04:00
|
|
|
(if (not session)
|
|
|
|
;; external process evaluation
|
2010-03-05 06:29:30 -05:00
|
|
|
(case result-type
|
2010-07-04 16:31:34 -04:00
|
|
|
(output (org-babel-eval org-babel-R-command body))
|
2010-03-05 06:29:30 -05:00
|
|
|
(value
|
2010-07-04 16:31:34 -04:00
|
|
|
(let ((tmp-file (make-temp-file "org-babel-R-results-")))
|
|
|
|
(org-babel-eval org-babel-R-command
|
|
|
|
(format org-babel-R-wrapper-method
|
|
|
|
body tmp-file
|
|
|
|
(if row-names-p "TRUE" "FALSE")
|
|
|
|
(if column-names-p
|
|
|
|
(if row-names-p "NA" "TRUE")
|
|
|
|
"FALSE")))
|
2009-07-19 12:01:00 -04:00
|
|
|
(org-babel-R-process-value-result
|
2010-07-04 16:31:34 -04:00
|
|
|
(org-babel-import-elisp-from-file
|
2010-07-04 18:09:20 -04:00
|
|
|
(org-babel-maybe-remote-file tmp-file)) column-names-p))))
|
2009-06-15 14:49:06 -04:00
|
|
|
;; comint session evaluation
|
2010-07-04 16:31:34 -04:00
|
|
|
(case result-type
|
|
|
|
(value
|
|
|
|
(let ((tmp-file (make-temp-file "org-babel-R"))
|
|
|
|
broke)
|
|
|
|
(org-babel-comint-with-output (session org-babel-R-eoe-output)
|
|
|
|
(insert (mapconcat
|
|
|
|
#'org-babel-chomp
|
|
|
|
(list
|
|
|
|
body
|
|
|
|
(format org-babel-R-wrapper-lastvar
|
|
|
|
tmp-file
|
|
|
|
(if row-names-p "TRUE" "FALSE")
|
|
|
|
(if column-names-p
|
|
|
|
(if row-names-p "NA" "TRUE")
|
|
|
|
"FALSE"))
|
|
|
|
org-babel-R-eoe-indicator) "\n"))
|
|
|
|
(inferior-ess-send-input))
|
|
|
|
(org-babel-R-process-value-result
|
|
|
|
(org-babel-import-elisp-from-file
|
|
|
|
(org-babel-maybe-remote-file tmp-file)) column-names-p)))
|
|
|
|
(output
|
|
|
|
(mapconcat
|
|
|
|
#'org-babel-chomp
|
|
|
|
(butlast
|
|
|
|
(delq nil
|
|
|
|
(mapcar
|
|
|
|
#'identity
|
|
|
|
(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)))) 2) "\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-06-11 20:12:15 -04:00
|
|
|
"R-specific processing of return value prior to return to
|
|
|
|
org-babel. Insert hline if column names in output have been
|
|
|
|
requested."
|
2009-07-19 12:01:00 -04:00
|
|
|
(if column-names-p
|
|
|
|
(cons (car result) (cons 'hline (cdr result)))
|
|
|
|
result))
|
|
|
|
|
2009-04-03 19:50:57 -04:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
(provide 'ob-R)
|
2010-06-25 12:32:35 -04:00
|
|
|
|
|
|
|
;; arch-tag: cd4c7298-503b-450f-a3c2-f3e74b630237
|
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
;;; ob-R.el ends here
|