ob-R: make it work with older ESS versions
[org-mode/org-tableheadings.git] / lisp / ob-R.el
blobd7ee397e572e5167c030bd76502d222cb3569604
1 ;;; ob-R.el --- org-babel functions for R code evaluation
3 ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Dan Davison
7 ;; Keywords: literate programming, reproducible research, R, statistics
8 ;; Homepage: http://orgmode.org
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 (eval-when-compile (require 'cl))
33 (declare-function orgtbl-to-tsv "org-table" (table params))
34 (declare-function R "ext:essd-r" (&optional start-args))
35 (declare-function inferior-ess-send-input "ext:ess-inf" ())
36 (declare-function ess-make-buffer-current "ext:ess-inf" ())
37 (declare-function ess-eval-buffer "ext:ess-inf" (vis))
38 (declare-function ess-wait-for-process "ext:ess-inf"
39 (proc &optional sec-prompt wait force-redisplay))
40 (declare-function org-number-sequence "org-compat" (from &optional to inc))
41 (declare-function org-remove-if-not "org" (predicate seq))
42 (declare-function org-every "org" (pred seq))
44 (defconst org-babel-header-args:R
45 '((width . :any)
46 (height . :any)
47 (bg . :any)
48 (units . :any)
49 (pointsize . :any)
50 (antialias . :any)
51 (quality . :any)
52 (compression . :any)
53 (res . :any)
54 (type . :any)
55 (family . :any)
56 (title . :any)
57 (fonts . :any)
58 (version . :any)
59 (paper . :any)
60 (encoding . :any)
61 (pagecentre . :any)
62 (colormodel . :any)
63 (useDingbats . :any)
64 (horizontal . :any)
65 (results . ((file list vector table scalar verbatim)
66 (raw org html latex code pp wrap)
67 (replace silent append prepend)
68 (output value graphics))))
69 "R-specific header arguments.")
71 (defconst ob-R-safe-header-args
72 (append org-babel-safe-header-args
73 '(:width :height :bg :units :pointsize :antialias :quality
74 :compression :res :type :family :title :fonts
75 :version :paper :encoding :pagecentre :colormodel
76 :useDingbats :horizontal))
77 "Header args which are safe for R babel blocks.
79 See `org-babel-safe-header-args' for documentation of the format of
80 this variable.")
82 (defvar org-babel-default-header-args:R '())
83 (put 'org-babel-default-header-args:R 'safe-local-variable
84 (org-babel-header-args-safe-fn ob-R-safe-header-args))
86 (defcustom org-babel-R-command "R --slave --no-save"
87 "Name of command to use for executing R code."
88 :group 'org-babel
89 :version "24.1"
90 :type 'string)
92 (defvar ess-local-process-name) ; dynamically scoped
93 (defun org-babel-edit-prep:R (info)
94 (let ((session (cdr (assoc :session (nth 2 info)))))
95 (when (and session (string-match "^\\*\\(.+?\\)\\*$" session))
96 (save-match-data (org-babel-R-initiate-session session nil)))))
98 (defconst ob-R-transfer-variable-table-with-header
99 "%s <- local({
100 con <- textConnection(
103 res <- read.table(
104 con,
105 header = %s,
106 row.names = %s,
107 sep = \"\\t\",
108 as.is = TRUE
110 close(con)
113 "R code used to transfer a table defined as a variable from org to R.
114 This function is used when the table contains a header.")
116 (defconst ob-R-transfer-variable-table-without-header
117 "%s <- local({
118 con <- textConnection(
121 res <- read.table(
122 con,
123 header = %s,
124 row.names = %s,
125 sep = \"\\t\",
126 as.is = TRUE,
127 fill = TRUE,
128 col.names = paste(\"V\", seq_len(%d), sep =\"\")
130 close(con)
133 "R code used to transfer a table defined as a variable from org to R.
134 This function is used when the table does not contain a header.")
136 (defun org-babel-expand-body:R (body params &optional graphics-file)
137 "Expand BODY according to PARAMS, return the expanded body."
138 (mapconcat 'identity
139 (append
140 (when (cdr (assoc :prologue params))
141 (list (cdr (assoc :prologue params))))
142 (org-babel-variable-assignments:R params)
143 (list body)
144 (when (cdr (assoc :epilogue params))
145 (list (cdr (assoc :epilogue params)))))
146 "\n"))
148 (defun org-babel-execute:R (body params)
149 "Execute a block of R code.
150 This function is called by `org-babel-execute-src-block'."
151 (save-excursion
152 (let* ((result-params (cdr (assoc :result-params params)))
153 (result-type (cdr (assoc :result-type params)))
154 (session (org-babel-R-initiate-session
155 (cdr (assoc :session params)) params))
156 (colnames-p (cdr (assoc :colnames params)))
157 (rownames-p (cdr (assoc :rownames params)))
158 (graphics-file (and (member "graphics" (assq :result-params params))
159 (org-babel-graphical-output-file params)))
160 (full-body
161 (let ((inside
162 (list (org-babel-expand-body:R body params graphics-file))))
163 (mapconcat 'identity
164 (if graphics-file
165 (append
166 (list (org-babel-R-construct-graphics-device-call
167 graphics-file params))
168 inside
169 (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()"))
170 inside)
171 "\n")))
172 (result
173 (org-babel-R-evaluate
174 session full-body result-type result-params
175 (or (equal "yes" colnames-p)
176 (org-babel-pick-name
177 (cdr (assoc :colname-names params)) colnames-p))
178 (or (equal "yes" rownames-p)
179 (org-babel-pick-name
180 (cdr (assoc :rowname-names params)) rownames-p)))))
181 (if graphics-file nil result))))
183 (defun org-babel-prep-session:R (session params)
184 "Prepare SESSION according to the header arguments specified in PARAMS."
185 (let* ((session (org-babel-R-initiate-session session params))
186 (var-lines (org-babel-variable-assignments:R params)))
187 (org-babel-comint-in-buffer session
188 (mapc (lambda (var)
189 (end-of-line 1) (insert var) (comint-send-input nil t)
190 (org-babel-comint-wait-for-output session)) var-lines))
191 session))
193 (defun org-babel-load-session:R (session body params)
194 "Load BODY into SESSION."
195 (save-window-excursion
196 (let ((buffer (org-babel-prep-session:R session params)))
197 (with-current-buffer buffer
198 (goto-char (process-mark (get-buffer-process (current-buffer))))
199 (insert (org-babel-chomp body)))
200 buffer)))
202 ;; helper functions
204 (defun org-babel-variable-assignments:R (params)
205 "Return list of R statements assigning the block's variables."
206 (let ((vars (mapcar 'cdr (org-babel-get-header params :var))))
207 (mapcar
208 (lambda (pair)
209 (org-babel-R-assign-elisp
210 (car pair) (cdr pair)
211 (equal "yes" (cdr (assoc :colnames params)))
212 (equal "yes" (cdr (assoc :rownames params)))))
213 (mapcar
214 (lambda (i)
215 (cons (car (nth i vars))
216 (org-babel-reassemble-table
217 (cdr (nth i vars))
218 (cdr (nth i (cdr (assoc :colname-names params))))
219 (cdr (nth i (cdr (assoc :rowname-names params)))))))
220 (org-number-sequence 0 (1- (length vars)))))))
222 (defun org-babel-R-quote-tsv-field (s)
223 "Quote field S for export to R."
224 (if (stringp s)
225 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
226 (format "%S" s)))
228 (defun org-babel-R-assign-elisp (name value colnames-p rownames-p)
229 "Construct R code assigning the elisp VALUE to a variable named NAME."
230 (if (listp value)
231 (let* ((lengths (mapcar 'length (org-remove-if-not 'sequencep value)))
232 (max (if lengths (apply 'max lengths) 0))
233 (min (if lengths (apply 'min lengths) 0)))
234 ;; Ensure VALUE has an orgtbl structure (depth of at least 2).
235 (unless (listp (car value)) (setq value (list value)))
236 (let ((file (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
237 (header (if (or (eq (nth 1 value) 'hline) colnames-p)
238 "TRUE" "FALSE"))
239 (row-names (if rownames-p "1" "NULL")))
240 (if (= max min)
241 (format ob-R-transfer-variable-table-with-header
242 name file header row-names)
243 (format ob-R-transfer-variable-table-without-header
244 name file header row-names max))))
245 (cond ((integerp value) (format "%s <- %s" name (concat (number-to-string value) "L")))
246 ((floatp value) (format "%s <- %s" name value))
247 ((stringp value) (format "%s <- %S" name (org-no-properties value)))
248 (t (format "%s <- %S" name (prin1-to-string value))))))
251 (defvar ess-ask-for-ess-directory) ; dynamically scoped
252 (defun org-babel-R-initiate-session (session params)
253 "If there is not a current R process then create one."
254 (unless (string= session "none")
255 (let ((session (or session "*R*"))
256 (ess-ask-for-ess-directory
257 (and (boundp 'ess-ask-for-ess-directory)
258 ess-ask-for-ess-directory
259 (not (cdr (assoc :dir params))))))
260 (if (org-babel-comint-buffer-livep session)
261 session
262 (save-window-excursion
263 (when (get-buffer session)
264 ;; Session buffer exists, but with dead process
265 (set-buffer session))
266 (require 'ess) (R)
267 (ess-wait-for-process
268 (get-process (or ess-local-process-name
269 ess-current-process-name)))
270 (rename-buffer
271 (if (bufferp session)
272 (buffer-name session)
273 (if (stringp session)
274 session
275 (buffer-name))))
276 (current-buffer))))))
278 (defun org-babel-R-associate-session (session)
279 "Associate R code buffer with an R session.
280 Make SESSION be the inferior ESS process associated with the
281 current code buffer."
282 (setq ess-local-process-name
283 (process-name (get-buffer-process session)))
284 (ess-make-buffer-current))
286 (defvar org-babel-R-graphics-devices
287 '((:bmp "bmp" "filename")
288 (:jpg "jpeg" "filename")
289 (:jpeg "jpeg" "filename")
290 (:tikz "tikz" "file")
291 (:tiff "tiff" "filename")
292 (:png "png" "filename")
293 (:svg "svg" "file")
294 (:pdf "pdf" "file")
295 (:ps "postscript" "file")
296 (:postscript "postscript" "file"))
297 "An alist mapping graphics file types to R functions.
299 Each member of this list is a list with three members:
300 1. the file extension of the graphics file, as an elisp :keyword
301 2. the R graphics device function to call to generate such a file
302 3. the name of the argument to this function which specifies the
303 file to write to (typically \"file\" or \"filename\")")
305 (defun org-babel-R-construct-graphics-device-call (out-file params)
306 "Construct the call to the graphics device."
307 (let* ((allowed-args '(:width :height :bg :units :pointsize
308 :antialias :quality :compression :res
309 :type :family :title :fonts :version
310 :paper :encoding :pagecentre :colormodel
311 :useDingbats :horizontal))
312 (device (and (string-match ".+\\.\\([^.]+\\)" out-file)
313 (match-string 1 out-file)))
314 (device-info (or (assq (intern (concat ":" device))
315 org-babel-R-graphics-devices)
316 (assq :png org-babel-R-graphics-devices)))
317 (extra-args (cdr (assq :R-dev-args params))) filearg args)
318 (setq device (nth 1 device-info))
319 (setq filearg (nth 2 device-info))
320 (setq args (mapconcat
321 (lambda (pair)
322 (if (member (car pair) allowed-args)
323 (format ",%s=%S"
324 (substring (symbol-name (car pair)) 1)
325 (cdr pair)) ""))
326 params ""))
327 (format "%s(%s=\"%s\"%s%s%s); tryCatch({"
328 device filearg out-file args
329 (if extra-args "," "") (or extra-args ""))))
331 (defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
332 (defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
334 (defvar org-babel-R-write-object-command "{function(object,transfer.file){object;invisible(if(inherits(try({tfile<-tempfile();write.table(object,file=tfile,sep=\"\\t\",na=\"nil\",row.names=%s,col.names=%s,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\")")
336 (defun org-babel-R-evaluate
337 (session body result-type result-params column-names-p row-names-p)
338 "Evaluate R code in BODY."
339 (if session
340 (org-babel-R-evaluate-session
341 session body result-type result-params column-names-p row-names-p)
342 (org-babel-R-evaluate-external-process
343 body result-type result-params column-names-p row-names-p)))
345 (defun org-babel-R-evaluate-external-process
346 (body result-type result-params column-names-p row-names-p)
347 "Evaluate BODY in external R process.
348 If RESULT-TYPE equals 'output then return standard output as a
349 string. If RESULT-TYPE equals 'value then return the value of the
350 last statement in BODY, as elisp."
351 (case result-type
352 (value
353 (let ((tmp-file (org-babel-temp-file "R-")))
354 (org-babel-eval org-babel-R-command
355 (format org-babel-R-write-object-command
356 (if row-names-p "TRUE" "FALSE")
357 (if column-names-p
358 (if row-names-p "NA" "TRUE")
359 "FALSE")
360 (format "{function ()\n{\n%s\n}}()" body)
361 (org-babel-process-file-name tmp-file 'noquote)))
362 (org-babel-R-process-value-result
363 (org-babel-result-cond result-params
364 (with-temp-buffer
365 (insert-file-contents tmp-file)
366 (buffer-string))
367 (org-babel-import-elisp-from-file tmp-file '(16)))
368 column-names-p)))
369 (output (org-babel-eval org-babel-R-command body))))
371 (defvar ess-eval-visibly-p)
373 (defun org-babel-R-evaluate-session
374 (session body result-type result-params column-names-p row-names-p)
375 "Evaluate BODY in SESSION.
376 If RESULT-TYPE equals 'output then return standard output as a
377 string. If RESULT-TYPE equals 'value then return the value of the
378 last statement in BODY, as elisp."
379 (case result-type
380 (value
381 (with-temp-buffer
382 (insert (org-babel-chomp body))
383 (let ((ess-local-process-name
384 (process-name (get-buffer-process session)))
385 (ess-eval-visibly-p nil))
386 (ess-eval-buffer nil)))
387 (let ((tmp-file (org-babel-temp-file "R-")))
388 (org-babel-comint-eval-invisibly-and-wait-for-file
389 session tmp-file
390 (format org-babel-R-write-object-command
391 (if row-names-p "TRUE" "FALSE")
392 (if column-names-p
393 (if row-names-p "NA" "TRUE")
394 "FALSE")
395 ".Last.value" (org-babel-process-file-name tmp-file 'noquote)))
396 (org-babel-R-process-value-result
397 (org-babel-result-cond result-params
398 (with-temp-buffer
399 (insert-file-contents tmp-file)
400 (buffer-string))
401 (org-babel-import-elisp-from-file tmp-file '(16)))
402 column-names-p)))
403 (output
404 (mapconcat
405 'org-babel-chomp
406 (butlast
407 (delq nil
408 (mapcar
409 (lambda (line) (when (> (length line) 0) line))
410 (mapcar
411 (lambda (line) ;; cleanup extra prompts left in output
412 (if (string-match
413 "^\\([ ]*[>+\\.][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line)
414 (substring line (match-end 1))
415 line))
416 (org-babel-comint-with-output (session org-babel-R-eoe-output)
417 (insert (mapconcat 'org-babel-chomp
418 (list body org-babel-R-eoe-indicator)
419 "\n"))
420 (inferior-ess-send-input)))))) "\n"))))
422 (defun org-babel-R-process-value-result (result column-names-p)
423 "R-specific processing of return value.
424 Insert hline if column names in output have been requested."
425 (if column-names-p
426 (cons (car result) (cons 'hline (cdr result)))
427 result))
429 (provide 'ob-R)
433 ;;; ob-R.el ends here