babel: all languages compiling cleanly (but R has a small problem)
[org-mode.git] / lisp / ob-R.el
blob06bc26ef57c45fcb38c92df719024d7c00268e1c
1 ;;; ob-R.el --- org-babel functions for R code evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research, R, statistics
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating R code
29 ;;; Code:
30 (require 'ob)
31 (require 'ob-ref)
32 (eval-when-compile (require 'cl))
34 (declare-function org-babel-comint-in-buffer "ob-comint" (buffer &rest body))
35 (declare-function comint-send-input "ob-comint" (el'.))
36 (declare-function org-babel-comint-wait-for-output "ob-comint" (buffer))
37 (declare-function org-babel-comint-buffer-livep "ob-comint" (buffer))
38 (declare-function org-babel-comint-with-output "ob-comint" (meta &rest body))
39 (declare-function orgtbl-to-tsv "ob-table" (table params))
40 (declare-function R "essd-r" (&optional start-args))
41 (declare-function inferior-ess-send-input "ess-inf" ())
43 (defconst org-babel-header-arg-names:R
44 '(width height bg units pointsize antialias quality compression
45 res type family title fonts version paper encoding
46 pagecentre colormodel useDingbats horizontal)
47 "R-specific header arguments.")
49 (defvar org-babel-default-header-args:R '())
51 (defun org-babel-expand-body:R (body params &optional processed-params)
52 "Expand BODY according to PARAMS, return the expanded body."
53 (let* ((processed-params (or processed-params
54 (org-babel-process-params params)))
55 (vars (mapcar (lambda (i) (cons (car (nth i (nth 1 processed-params)))
56 (org-babel-reassemble-table
57 (cdr (nth i (nth 1 processed-params)))
58 (cdr (nth i (nth 4 processed-params)))
59 (cdr (nth i (nth 5 processed-params))))))
60 (number-sequence 0 (1- (length (nth 1 processed-params))))))
61 (out-file (cdr (assoc :file params))))
62 (concat
63 (if out-file (concat (org-babel-R-construct-graphics-device-call out-file params) "\n") "")
64 (mapconcat ;; define any variables
65 (lambda (pair)
66 (org-babel-R-assign-elisp (car pair) (cdr pair)
67 (equal "yes" (cdr (assoc :colnames params)))
68 (equal "yes" (cdr (assoc :rownames params)))))
69 vars "\n")
70 "\n" body "\n" (if out-file "dev.off()\n" ""))))
72 (defun org-babel-execute:R (body params)
73 "Execute a block of R code with org-babel. This function is
74 called by `org-babel-execute-src-block'."
75 (message "executing R source code block...")
76 (save-excursion
77 (let* ((processed-params (org-babel-process-params params))
78 (result-type (nth 3 processed-params))
79 (session (org-babel-R-initiate-session (first processed-params) params))
80 (colnames-p (cdr (assoc :colnames params)))
81 (rownames-p (cdr (assoc :rownames params)))
82 (out-file (cdr (assoc :file params)))
83 (full-body (org-babel-expand-body:R body params processed-params))
84 (result
85 (org-babel-R-evaluate
86 session full-body result-type
87 (or (equal "yes" colnames-p)
88 (org-babel-pick-name (nth 4 processed-params) colnames-p))
89 (or (equal "yes" rownames-p)
90 (org-babel-pick-name (nth 5 processed-params) rownames-p)))))
91 (or out-file result))))
93 (defun org-babel-prep-session:R (session params)
94 "Prepare SESSION according to the header arguments specified in PARAMS."
95 (let* ((session (org-babel-R-initiate-session session params))
96 (vars (org-babel-ref-variables params))
97 (var-lines
98 (mapcar
99 (lambda (pair) (org-babel-R-assign-elisp
100 (car pair) (cdr pair)
101 (equal (cdr (assoc :colnames params)) "yes")
102 (equal (cdr (assoc :rownames params)) "yes")))
103 vars)))
104 (org-babel-comint-in-buffer session
105 (mapc (lambda (var)
106 (end-of-line 1) (insert var) (comint-send-input nil t)
107 (org-babel-comint-wait-for-output session)) var-lines))
108 session))
110 (defun org-babel-load-session:R (session body params)
111 "Load BODY into SESSION."
112 (save-window-excursion
113 (let ((buffer (org-babel-prep-session:R session params)))
114 (with-current-buffer buffer
115 (goto-char (process-mark (get-buffer-process (current-buffer))))
116 (insert (org-babel-chomp body)))
117 buffer)))
119 ;; helper functions
121 (defun org-babel-R-quote-tsv-field (s)
122 "Quote field S for export to R."
123 (if (stringp s)
124 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
125 (format "%S" s)))
127 (defun org-babel-R-assign-elisp (name value colnames-p rownames-p)
128 "Construct R code assigning the elisp VALUE to a variable named NAME."
129 (if (listp value)
130 (let ((transition-file (make-temp-file "org-babel-R-import")))
131 ;; ensure VALUE has an orgtbl structure (depth of at least 2)
132 (unless (listp (car value)) (setq value (list value)))
133 (with-temp-file (org-babel-maybe-remote-file transition-file)
134 (insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
135 (insert "\n"))
136 (format "%s <- read.table(\"%s\", header=%s, row.names=%s, sep=\"\\t\", as.is=TRUE)"
137 name transition-file
138 (if (or (eq (nth 1 value) 'hline) colnames-p) "TRUE" "FALSE")
139 (if rownames-p "1" "NULL")))
140 (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
142 (defun org-babel-R-initiate-session (session params)
143 "If there is not a current R process then create one."
144 (unless (string= session "none")
145 (let ((session (or session "*R*"))
146 (ess-ask-for-ess-directory (not (cdr (assoc :dir params)))))
147 (if (org-babel-comint-buffer-livep session)
148 session
149 (save-window-excursion
151 (rename-buffer (if (bufferp session) (buffer-name session)
152 (if (stringp session) session (buffer-name)))) (current-buffer))))))
154 (defun org-babel-R-construct-graphics-device-call (out-file params)
155 "Construct the call to the graphics device."
156 (let ((devices
157 '((:bmp . "bmp")
158 (:jpg . "jpeg")
159 (:jpeg . "jpeg")
160 (:tiff . "tiff")
161 (:png . "png")
162 (:svg . "svg")
163 (:pdf . "pdf")
164 (:ps . "postscript")
165 (:postscript . "postscript")))
166 (allowed-args '(:width :height :bg :units :pointsize
167 :antialias :quality :compression :res :type
168 :family :title :fonts :version :paper :encoding
169 :pagecentre :colormodel :useDingbats :horizontal))
170 (device (and (string-match ".+\\.\\([^.]+\\)" out-file) (match-string 1 out-file)))
171 (extra-args (cdr (assq :R-dev-args params))) filearg args)
172 (setq device (or (and device (cdr (assq (intern (concat ":" device)) devices))) "png"))
173 (setq filearg (if (member device '("pdf" "postscript" "svg")) "file" "filename"))
174 (setq args (mapconcat (lambda (pair)
175 (if (member (car pair) allowed-args)
176 (format ",%s=%s" (substring (symbol-name (car pair)) 1) (cdr pair)) ""))
177 params ""))
178 (format "%s(%s=\"%s\"%s%s%s)\n" device filearg out-file args (if extra-args "," "") (or extra-args ""))))
180 (defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
181 (defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
182 (defvar org-babel-R-wrapper-method "main <- function ()\n{\n%s\n}
183 write.table(main(), file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=%s, col.names=%s, quote=FALSE)")
185 (defvar inferior-ess-primary-prompt)
186 (defvar inferior-ess-secondary-prompt)
187 (defun org-babel-R-evaluate (session body result-type column-names-p row-names-p)
188 "Pass BODY to the R process in SESSION. If RESULT-TYPE equals
189 'output then return a list of the outputs of the statements in
190 BODY, if RESULT-TYPE equals 'value then return the value of the
191 last statement in BODY, as elisp."
192 (if (not session)
193 ;; external process evaluation
194 (case result-type
195 (output
196 (with-temp-buffer
197 (insert body)
198 (org-babel-shell-command-on-region (point-min) (point-max) "R --slave --no-save" 'current-buffer 'replace)
199 (org-babel-trim (buffer-string))))
200 (value
201 (let* ((tmp-file (make-temp-file "R-out-functional-results")) exit-code
202 (stderr
203 (with-temp-buffer
204 (insert (format org-babel-R-wrapper-method
205 body tmp-file (if row-names-p "TRUE" "FALSE") (if column-names-p (if row-names-p "NA" "TRUE") "FALSE")))
206 (setq exit-code (org-babel-shell-command-on-region
207 (point-min) (point-max) "R --no-save" nil 'replace (current-buffer)))
208 (buffer-string))))
209 (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
210 (org-babel-R-process-value-result
211 (org-babel-import-elisp-from-file (org-babel-maybe-remote-file tmp-file))
212 column-names-p))))
213 ;; comint session evaluation
214 (org-babel-comint-in-buffer session
215 (let* ((tmp-file (make-temp-file "org-babel-R"))
216 (full-body
217 (case result-type
218 (value
219 (mapconcat #'org-babel-chomp (list body
220 (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=%s, col.names=%s, quote=FALSE)" tmp-file (if row-names-p "TRUE" "FALSE") (if column-names-p (if row-names-p "NA" "TRUE") "FALSE"))
221 org-babel-R-eoe-indicator) "\n"))
222 (output
223 (mapconcat #'org-babel-chomp (list body org-babel-R-eoe-indicator) "\n"))))
224 (raw
225 (org-babel-comint-with-output (session org-babel-R-eoe-output)
226 (insert full-body) (inferior-ess-send-input)))
227 (comint-prompt-regexp
228 (concat "^\\("
229 inferior-ess-primary-prompt
230 "\\|"
231 inferior-ess-secondary-prompt
232 "\\)*"))
233 broke results)
234 (case result-type
235 (value (org-babel-R-process-value-result
236 (org-babel-import-elisp-from-file
237 (org-babel-maybe-remote-file tmp-file))
238 column-names-p))
239 (output
240 (flet ((extractor
241 (el)
242 (if (or broke
243 (and (string-match (regexp-quote org-babel-R-eoe-output) el)
244 (setq broke t)))
246 (if (= (length el) 0)
248 (if (string-match comint-prompt-regexp el)
249 (org-babel-trim (substring el (match-end 0)))
250 el)))))
251 (mapconcat
252 #'identity
253 (delete nil (mapcar #'extractor (mapcar #'org-babel-chomp raw))) "\n"))))))))
255 (defun org-babel-R-process-value-result (result column-names-p)
256 "R-specific processing of return value prior to return to
257 org-babel. Insert hline if column names in output have been
258 requested."
259 (if column-names-p
260 (cons (car result) (cons 'hline (cdr result)))
261 result))
264 (provide 'ob-R)
266 ;; arch-tag: cd4c7298-503b-450f-a3c2-f3e74b630237
268 ;;; ob-R.el ends here