2010-06-11 23:02:42 +00:00
|
|
|
;;; ob-R.el --- org-babel functions for R code evaluation
|
2009-03-25 21:42:57 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
|
2009-03-25 21:42:57 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; Author: Eric Schulte, Dan Davison
|
2009-03-29 22:09:06 +00:00
|
|
|
;; Keywords: literate programming, reproducible research, R, statistics
|
2009-03-25 21:42:57 +00:00
|
|
|
;; Homepage: http://orgmode.org
|
2010-07-19 06:33:24 +00:00
|
|
|
;; Version: 7.01trans
|
2009-03-25 21:42:57 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-03-25 21:42:57 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-03-25 21:42:57 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 16:32:35 +00: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 21:42:57 +00: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 16:32:35 +00:00
|
|
|
|
2009-03-25 21:42:57 +00:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2010-06-25 16:32:35 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2009-03-25 21:42:57 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2009-05-24 20:38:01 +00:00
|
|
|
;; Org-Babel support for evaluating R code
|
2009-03-25 21:42:57 +00:00
|
|
|
|
|
|
|
;;; Code:
|
2010-06-11 23:02:42 +00:00
|
|
|
(require 'ob)
|
2010-07-03 02:21:31 +00:00
|
|
|
(require 'ob-ref)
|
2010-07-04 21:46:13 +00:00
|
|
|
(require 'ob-comint)
|
|
|
|
(require 'ob-eval)
|
2010-07-03 02:21:31 +00:00
|
|
|
(eval-when-compile (require 'cl))
|
|
|
|
|
2010-08-27 20:35:09 +00:00
|
|
|
(declare-function orgtbl-to-tsv "org-table" (table params))
|
2010-07-04 15:24:08 +00:00
|
|
|
(declare-function R "ext:essd-r" (&optional start-args))
|
|
|
|
(declare-function inferior-ess-send-input "ext:ess-inf" ())
|
2010-08-27 20:42:24 +00:00
|
|
|
(declare-function ess-make-buffer-current "ext:ess-inf" ())
|
2010-08-31 01:20:01 +00:00
|
|
|
(declare-function ess-eval-buffer "ext:ess-inf" (vis))
|
2009-03-25 21:42:57 +00:00
|
|
|
|
2010-05-28 13:52:00 +00: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 17:12:54 +00:00
|
|
|
(defvar org-babel-default-header-args:R '())
|
|
|
|
|
2010-07-04 20:31:34 +00:00
|
|
|
(defvar org-babel-R-command "R --slave --no-save"
|
|
|
|
"Name of command to use for executing R code.")
|
|
|
|
|
2010-04-20 16:16:21 +00:00
|
|
|
(defun org-babel-expand-body:R (body params &optional processed-params)
|
2010-06-12 00:12:15 +00:00
|
|
|
"Expand BODY according to PARAMS, return the expanded body."
|
2010-04-20 16:16:21 +00:00
|
|
|
(let* ((processed-params (or processed-params
|
|
|
|
(org-babel-process-params params)))
|
2010-07-04 20:31:34 +00: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 16:16:21 +00:00
|
|
|
(out-file (cdr (assoc :file params))))
|
2010-07-04 20:31:34 +00:00
|
|
|
(mapconcat ;; define any variables
|
|
|
|
#'org-babel-trim
|
|
|
|
((lambda (inside)
|
|
|
|
(if out-file
|
|
|
|
(append
|
2010-07-05 17:10:11 +00:00
|
|
|
(list (org-babel-R-construct-graphics-device-call out-file params))
|
2010-07-04 20:31:34 +00: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 16:16:21 +00:00
|
|
|
|
2009-07-08 04:13:07 +00:00
|
|
|
(defun org-babel-execute:R (body params)
|
2010-07-13 22:56:17 +00:00
|
|
|
"Execute a block of R code.
|
|
|
|
This function is called by `org-babel-execute-src-block'."
|
2010-02-22 18:37:16 +00:00
|
|
|
(save-excursion
|
2009-11-10 22:22:25 +00:00
|
|
|
(let* ((processed-params (org-babel-process-params params))
|
2010-06-25 17:26:48 +00:00
|
|
|
(result-type (nth 3 processed-params))
|
2010-07-04 20:31:34 +00:00
|
|
|
(session (org-babel-R-initiate-session
|
|
|
|
(first processed-params) params))
|
2010-04-30 18:14:13 +00: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-20 01:00:24 +00:00
|
|
|
(out-file (cdr (assoc :file params)))
|
2010-04-20 16:16:21 +00:00
|
|
|
(full-body (org-babel-expand-body:R body params processed-params))
|
2010-04-30 18:14:13 +00: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 20:31:34 +00: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-20 01:00:24 +00:00
|
|
|
(or out-file result))))
|
2009-04-04 23:23:35 +00:00
|
|
|
|
2009-06-14 18:48:11 +00:00
|
|
|
(defun org-babel-prep-session:R (session params)
|
|
|
|
"Prepare SESSION according to the header arguments specified in PARAMS."
|
2010-03-13 17:59:41 +00:00
|
|
|
(let* ((session (org-babel-R-initiate-session session params))
|
2010-04-08 19:26:08 +00:00
|
|
|
(vars (org-babel-ref-variables params))
|
|
|
|
(var-lines
|
|
|
|
(mapcar
|
2010-04-19 21:25:43 +00:00
|
|
|
(lambda (pair) (org-babel-R-assign-elisp
|
2010-07-04 20:31:34 +00:00
|
|
|
(car pair) (cdr pair)
|
|
|
|
(equal (cdr (assoc :colnames params)) "yes")
|
|
|
|
(equal (cdr (assoc :rownames params)) "yes")))
|
2010-04-19 21:25:43 +00:00
|
|
|
vars)))
|
2010-04-08 19:26:08 +00: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 19:22:50 +00:00
|
|
|
(end-of-line 1) (insert var) (comint-send-input nil t)
|
2010-04-08 19:26:08 +00:00
|
|
|
(org-babel-comint-wait-for-output session)) var-lines))
|
2010-01-11 17:14:30 +00: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 18:48:11 +00:00
|
|
|
|
|
|
|
;; helper functions
|
|
|
|
|
2009-05-24 20:38:01 +00:00
|
|
|
(defun org-babel-R-quote-tsv-field (s)
|
2009-04-04 23:23:35 +00:00
|
|
|
"Quote field S for export to R."
|
2009-05-22 14:57:35 +00:00
|
|
|
(if (stringp s)
|
|
|
|
(concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
|
|
|
|
(format "%S" s)))
|
2009-04-04 23:23:35 +00:00
|
|
|
|
2010-04-19 21:25:43 +00:00
|
|
|
(defun org-babel-R-assign-elisp (name value colnames-p rownames-p)
|
2010-04-08 19:26:08 +00:00
|
|
|
"Construct R code assigning the elisp VALUE to a variable named NAME."
|
2009-06-15 19:17:58 +00:00
|
|
|
(if (listp value)
|
2010-08-25 20:43:07 +00:00
|
|
|
(let ((transition-file (org-babel-temp-file "R-import-")))
|
2009-06-15 19:17:58 +00:00
|
|
|
;; ensure VALUE has an orgtbl structure (depth of at least 2)
|
|
|
|
(unless (listp (car value)) (setq value (list value)))
|
2010-02-25 16:03:57 +00:00
|
|
|
(with-temp-file (org-babel-maybe-remote-file transition-file)
|
2009-06-15 19:17:58 +00:00
|
|
|
(insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
|
|
|
|
(insert "\n"))
|
2010-04-19 21:25:43 +00:00
|
|
|
(format "%s <- read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE)"
|
|
|
|
name transition-file
|
2010-06-25 17:26:48 +00:00
|
|
|
(if (or (eq (nth 1 value) 'hline) colnames-p) "TRUE" "FALSE")
|
2010-04-19 21:25:43 +00:00
|
|
|
(if rownames-p "1" "NULL")))
|
2009-06-15 19:17:58 +00:00
|
|
|
(format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
|
2009-05-18 00:42:04 +00:00
|
|
|
|
2010-09-05 15:41:23 +00:00
|
|
|
(defvar ess-ask-for-ess-directory nil)
|
2010-03-13 17:59:41 +00:00
|
|
|
(defun org-babel-R-initiate-session (session params)
|
2009-06-12 00:04:42 +00:00
|
|
|
"If there is not a current R process then create one."
|
2009-06-15 18:49:06 +00:00
|
|
|
(unless (string= session "none")
|
2010-03-13 17:59:41 +00:00
|
|
|
(let ((session (or session "*R*"))
|
2010-08-24 03:06:55 +00:00
|
|
|
(ess-ask-for-ess-directory
|
|
|
|
(and ess-ask-for-ess-directory (not (cdr (assoc :dir params))))))
|
2010-03-13 17:59:41 +00:00
|
|
|
(if (org-babel-comint-buffer-livep session)
|
|
|
|
session
|
|
|
|
(save-window-excursion
|
2010-07-04 20:31:34 +00:00
|
|
|
(require 'ess) (R)
|
|
|
|
(rename-buffer
|
|
|
|
(if (bufferp session)
|
|
|
|
(buffer-name session)
|
|
|
|
(if (stringp session)
|
|
|
|
session
|
|
|
|
(buffer-name))))
|
|
|
|
(current-buffer))))))
|
2009-06-12 00:04:42 +00:00
|
|
|
|
2010-09-05 15:41:23 +00:00
|
|
|
(defvar ess-local-process-name nil)
|
2010-08-17 20:44:28 +00: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."
|
|
|
|
(setq ess-local-process-name
|
|
|
|
(process-name (get-buffer-process session)))
|
|
|
|
(ess-make-buffer-current))
|
|
|
|
|
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-20 01:00:24 +00:00
|
|
|
(defun org-babel-R-construct-graphics-device-call (out-file params)
|
2010-06-12 00:12:15 +00: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-20 01:00:24 +00:00
|
|
|
(let ((devices
|
|
|
|
'((:bmp . "bmp")
|
|
|
|
(:jpg . "jpeg")
|
|
|
|
(:jpeg . "jpeg")
|
|
|
|
(:tiff . "tiff")
|
|
|
|
(:png . "png")
|
2010-04-01 22:45:59 +00: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-20 01:00:24 +00:00
|
|
|
(:pdf . "pdf")
|
|
|
|
(:ps . "postscript")
|
|
|
|
(:postscript . "postscript")))
|
|
|
|
(allowed-args '(:width :height :bg :units :pointsize
|
2010-07-04 20:31:34 +00: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-20 01:00:24 +00:00
|
|
|
(extra-args (cdr (assq :R-dev-args params))) filearg args)
|
2010-07-04 20:31:34 +00: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-20 01:00:24 +00:00
|
|
|
|
2009-06-12 00:04:42 +00:00
|
|
|
(defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
|
|
|
|
(defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
|
2010-08-28 12:52:37 +00:00
|
|
|
(defvar org-babel-R-write-object-command "{function(object, transfer.file) {invisible(if(inherits(try(write.table(object, file=transfer.file, sep=\"\\t\", na=\"nil\",row.names=%s, col.names=%s, quote=FALSE), silent=TRUE),\"try-error\")) {if(!file.exists(transfer.file)) file.create(transfer.file)})}}(object=%s, transfer.file=\"%s\")")
|
2009-06-12 00:04:42 +00:00
|
|
|
|
2010-07-04 20:31:34 +00:00
|
|
|
(defun org-babel-R-evaluate
|
|
|
|
(session body result-type column-names-p row-names-p)
|
2010-08-19 00:20:54 +00:00
|
|
|
"Evaluate R code in BODY."
|
|
|
|
(if session
|
|
|
|
(org-babel-R-evaluate-session
|
|
|
|
session body result-type column-names-p row-names-p)
|
|
|
|
(org-babel-R-evaluate-external-process
|
|
|
|
body result-type column-names-p row-names-p)))
|
|
|
|
|
|
|
|
(defun org-babel-R-evaluate-external-process
|
|
|
|
(body result-type column-names-p row-names-p)
|
|
|
|
"Evaluate BODY in external R process.
|
|
|
|
If RESULT-TYPE equals 'output then return standard output as a
|
|
|
|
string. If RESULT-TYPE equals 'value then return the value of the
|
|
|
|
last statement in BODY, as elisp."
|
|
|
|
(case result-type
|
|
|
|
(value
|
2010-08-30 16:34:05 +00:00
|
|
|
(let ((tmp-file (org-babel-temp-file "R-")))
|
2010-08-19 00:20:54 +00:00
|
|
|
(org-babel-eval org-babel-R-command
|
2010-08-28 12:52:37 +00:00
|
|
|
(format org-babel-R-write-object-command
|
2010-08-19 00:20:54 +00:00
|
|
|
(if row-names-p "TRUE" "FALSE")
|
|
|
|
(if column-names-p
|
|
|
|
(if row-names-p "NA" "TRUE")
|
2010-08-28 12:52:37 +00:00
|
|
|
"FALSE")
|
|
|
|
(format "{function ()\n{\n%s\n}}()" body)
|
2010-08-30 16:34:05 +00:00
|
|
|
(org-babel-tramp-localname tmp-file)))
|
2010-08-19 00:20:54 +00:00
|
|
|
(org-babel-R-process-value-result
|
2010-08-30 16:34:05 +00:00
|
|
|
(org-babel-import-elisp-from-file tmp-file '(16)) column-names-p)))
|
2010-08-19 00:20:54 +00:00
|
|
|
(output (org-babel-eval org-babel-R-command body))))
|
|
|
|
|
|
|
|
(defun org-babel-R-evaluate-session
|
|
|
|
(session body result-type column-names-p row-names-p)
|
|
|
|
"Evaluate BODY in SESSION.
|
|
|
|
If RESULT-TYPE equals 'output then return standard output as a
|
|
|
|
string. If RESULT-TYPE equals 'value then return the value of the
|
|
|
|
last statement in BODY, as elisp."
|
|
|
|
(case result-type
|
|
|
|
(value
|
2010-08-30 16:26:02 +00:00
|
|
|
(with-temp-buffer
|
|
|
|
(insert (org-babel-chomp body))
|
|
|
|
(let ((ess-local-process-name
|
|
|
|
(process-name (get-buffer-process session))))
|
|
|
|
(ess-eval-buffer nil)))
|
|
|
|
(let ((tmp-file (org-babel-temp-file "R-")))
|
|
|
|
(org-babel-comint-eval-invisibly-and-wait-for-file
|
2010-08-30 16:34:05 +00:00
|
|
|
session tmp-file
|
2010-08-28 12:52:37 +00: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-08-30 16:34:05 +00:00
|
|
|
".Last.value" (org-babel-tramp-localname tmp-file)))
|
2010-08-19 00:20:54 +00:00
|
|
|
(org-babel-R-process-value-result
|
2010-08-30 16:34:05 +00:00
|
|
|
(org-babel-import-elisp-from-file tmp-file '(16)) column-names-p)))
|
2010-08-19 00:20:54 +00:00
|
|
|
(output
|
|
|
|
(mapconcat
|
|
|
|
#'org-babel-chomp
|
|
|
|
(butlast
|
|
|
|
(delq nil
|
|
|
|
(mapcar
|
|
|
|
(lambda (line) ;; cleanup extra prompts left in output
|
|
|
|
(if (string-match
|
|
|
|
"^\\([ ]*[>+][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line)
|
|
|
|
(substring line (match-end 1))
|
|
|
|
line))
|
|
|
|
(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 23:19:14 +00:00
|
|
|
|
2009-07-19 16:01:00 +00:00
|
|
|
(defun org-babel-R-process-value-result (result column-names-p)
|
2010-07-13 22:56:17 +00:00
|
|
|
"R-specific processing of return value.
|
|
|
|
Insert hline if column names in output have been requested."
|
2009-07-19 16:01:00 +00:00
|
|
|
(if column-names-p
|
|
|
|
(cons (car result) (cons 'hline (cdr result)))
|
|
|
|
result))
|
2009-04-03 23:50:57 +00:00
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
(provide 'ob-R)
|
2010-06-25 16:32:35 +00:00
|
|
|
|
|
|
|
;; arch-tag: cd4c7298-503b-450f-a3c2-f3e74b630237
|
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
;;; ob-R.el ends here
|