ob-exp: Escape code when re-creating a src blocks
[org-mode.git] / lisp / ob-R.el
blob3dedb39365461e786193b613bfc249db62a3cf68
1 ;;; ob-R.el --- org-babel functions for R code evaluation
3 ;; Copyright (C) 2009-2012 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 (require 'ob-ref)
32 (require 'ob-comint)
33 (require 'ob-eval)
34 (eval-when-compile (require 'cl))
36 (declare-function orgtbl-to-tsv "org-table" (table params))
37 (declare-function R "ext:essd-r" (&optional start-args))
38 (declare-function inferior-ess-send-input "ext:ess-inf" ())
39 (declare-function ess-make-buffer-current "ext:ess-inf" ())
40 (declare-function ess-eval-buffer "ext:ess-inf" (vis))
41 (declare-function org-number-sequence "org-compat" (from &optional to inc))
42 (declare-function org-remove-if-not "org" (predicate 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 (defvar org-babel-default-header-args:R '())
73 (defcustom org-babel-R-command "R --slave --no-save"
74 "Name of command to use for executing R code."
75 :group 'org-babel
76 :version "24.1"
77 :type 'string)
79 (defvar ess-local-process-name) ; dynamically scoped
80 (defun org-babel-edit-prep:R (info)
81 (let ((session (cdr (assoc :session (nth 2 info)))))
82 (when (and session (string-match "^\\*\\(.+?\\)\\*$" session))
83 (save-match-data (org-babel-R-initiate-session session nil)))))
85 (defun org-babel-expand-body:R (body params &optional graphics-file)
86 "Expand BODY according to PARAMS, return the expanded body."
87 (let ((graphics-file
88 (or graphics-file (org-babel-R-graphical-output-file params))))
89 (mapconcat
90 #'identity
91 ((lambda (inside)
92 (if graphics-file
93 (append
94 (list (org-babel-R-construct-graphics-device-call
95 graphics-file params))
96 inside
97 (list "dev.off()"))
98 inside))
99 (append (org-babel-variable-assignments:R params)
100 (list body))) "\n")))
102 (defun org-babel-execute:R (body params)
103 "Execute a block of R code.
104 This function is called by `org-babel-execute-src-block'."
105 (save-excursion
106 (let* ((result-params (cdr (assoc :result-params params)))
107 (result-type (cdr (assoc :result-type params)))
108 (session (org-babel-R-initiate-session
109 (cdr (assoc :session params)) params))
110 (colnames-p (cdr (assoc :colnames params)))
111 (rownames-p (cdr (assoc :rownames params)))
112 (graphics-file (org-babel-R-graphical-output-file params))
113 (full-body (org-babel-expand-body:R body params graphics-file))
114 (result
115 (org-babel-R-evaluate
116 session full-body result-type result-params
117 (or (equal "yes" colnames-p)
118 (org-babel-pick-name
119 (cdr (assoc :colname-names params)) colnames-p))
120 (or (equal "yes" rownames-p)
121 (org-babel-pick-name
122 (cdr (assoc :rowname-names params)) rownames-p)))))
123 (if graphics-file nil result))))
125 (defun org-babel-prep-session:R (session params)
126 "Prepare SESSION according to the header arguments specified in PARAMS."
127 (let* ((session (org-babel-R-initiate-session session params))
128 (var-lines (org-babel-variable-assignments:R params)))
129 (org-babel-comint-in-buffer session
130 (mapc (lambda (var)
131 (end-of-line 1) (insert var) (comint-send-input nil t)
132 (org-babel-comint-wait-for-output session)) var-lines))
133 session))
135 (defun org-babel-load-session:R (session body params)
136 "Load BODY into SESSION."
137 (save-window-excursion
138 (let ((buffer (org-babel-prep-session:R session params)))
139 (with-current-buffer buffer
140 (goto-char (process-mark (get-buffer-process (current-buffer))))
141 (insert (org-babel-chomp body)))
142 buffer)))
144 ;; helper functions
146 (defun org-babel-variable-assignments:R (params)
147 "Return list of R statements assigning the block's variables."
148 (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
149 (mapcar
150 (lambda (pair)
151 (org-babel-R-assign-elisp
152 (car pair) (cdr pair)
153 (equal "yes" (cdr (assoc :colnames params)))
154 (equal "yes" (cdr (assoc :rownames params)))))
155 (mapcar
156 (lambda (i)
157 (cons (car (nth i vars))
158 (org-babel-reassemble-table
159 (cdr (nth i vars))
160 (cdr (nth i (cdr (assoc :colname-names params))))
161 (cdr (nth i (cdr (assoc :rowname-names params)))))))
162 (org-number-sequence 0 (1- (length vars)))))))
164 (defun org-babel-R-quote-tsv-field (s)
165 "Quote field S for export to R."
166 (if (stringp s)
167 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
168 (format "%S" s)))
170 (defun org-babel-R-assign-elisp (name value colnames-p rownames-p)
171 "Construct R code assigning the elisp VALUE to a variable named NAME."
172 (if (listp value)
173 (let ((max (apply #'max (mapcar #'length (org-remove-if-not
174 #'sequencep value))))
175 (min (apply #'min (mapcar #'length (org-remove-if-not
176 #'sequencep value))))
177 (transition-file (org-babel-temp-file "R-import-")))
178 ;; ensure VALUE has an orgtbl structure (depth of at least 2)
179 (unless (listp (car value)) (setq value (list value)))
180 (with-temp-file transition-file
181 (insert
182 (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field))
183 "\n"))
184 (let ((file (org-babel-process-file-name transition-file 'noquote))
185 (header (if (or (eq (nth 1 value) 'hline) colnames-p)
186 "TRUE" "FALSE"))
187 (row-names (if rownames-p "1" "NULL")))
188 (if (= max min)
189 (format "%s <- read.table(\"%s\",
190 header=%s,
191 row.names=%s,
192 sep=\"\\t\",
193 as.is=TRUE)" name file header row-names)
194 (format "%s <- read.table(\"%s\",
195 header=%s,
196 row.names=%s,
197 sep=\"\\t\",
198 as.is=TRUE,
199 fill=TRUE,
200 col.names = paste(\"V\", seq_len(%d), sep =\"\"))"
201 name file header row-names max))))
202 (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
204 (defvar ess-ask-for-ess-directory) ; dynamically scoped
205 (defun org-babel-R-initiate-session (session params)
206 "If there is not a current R process then create one."
207 (unless (string= session "none")
208 (let ((session (or session "*R*"))
209 (ess-ask-for-ess-directory
210 (and (and (boundp 'ess-ask-for-ess-directory) ess-ask-for-ess-directory)
211 (not (cdr (assoc :dir params))))))
212 (if (org-babel-comint-buffer-livep session)
213 session
214 (save-window-excursion
215 (require 'ess) (R)
216 (rename-buffer
217 (if (bufferp session)
218 (buffer-name session)
219 (if (stringp session)
220 session
221 (buffer-name))))
222 (current-buffer))))))
224 (defun org-babel-R-associate-session (session)
225 "Associate R code buffer with an R session.
226 Make SESSION be the inferior ESS process associated with the
227 current code buffer."
228 (setq ess-local-process-name
229 (process-name (get-buffer-process session)))
230 (ess-make-buffer-current))
232 (defun org-babel-R-graphical-output-file (params)
233 "Name of file to which R should send graphical output."
234 (and (member "graphics" (cdr (assq :result-params params)))
235 (cdr (assq :file params))))
237 (defun org-babel-R-construct-graphics-device-call (out-file params)
238 "Construct the call to the graphics device."
239 (let ((devices
240 '((:bmp . "bmp")
241 (:jpg . "jpeg")
242 (:jpeg . "jpeg")
243 (:tex . "tikz")
244 (:tiff . "tiff")
245 (:png . "png")
246 (:svg . "svg")
247 (:pdf . "pdf")
248 (:ps . "postscript")
249 (:postscript . "postscript")))
250 (allowed-args '(:width :height :bg :units :pointsize
251 :antialias :quality :compression :res
252 :type :family :title :fonts :version
253 :paper :encoding :pagecentre :colormodel
254 :useDingbats :horizontal))
255 (device (and (string-match ".+\\.\\([^.]+\\)" out-file)
256 (match-string 1 out-file)))
257 (extra-args (cdr (assq :R-dev-args params))) filearg args)
258 (setq device (or (and device (cdr (assq (intern (concat ":" device))
259 devices))) "png"))
260 (setq filearg
261 (if (member device '("pdf" "postscript" "svg" "tikz")) "file" "filename"))
262 (setq args (mapconcat
263 (lambda (pair)
264 (if (member (car pair) allowed-args)
265 (format ",%s=%S"
266 (substring (symbol-name (car pair)) 1)
267 (cdr pair)) ""))
268 params ""))
269 (format "%s(%s=\"%s\"%s%s%s)"
270 device filearg out-file args
271 (if extra-args "," "") (or extra-args ""))))
273 (defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
274 (defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
276 (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\")")
278 (defun org-babel-R-evaluate
279 (session body result-type result-params column-names-p row-names-p)
280 "Evaluate R code in BODY."
281 (if session
282 (org-babel-R-evaluate-session
283 session body result-type result-params column-names-p row-names-p)
284 (org-babel-R-evaluate-external-process
285 body result-type result-params column-names-p row-names-p)))
287 (defun org-babel-R-evaluate-external-process
288 (body result-type result-params column-names-p row-names-p)
289 "Evaluate BODY in external R process.
290 If RESULT-TYPE equals 'output then return standard output as a
291 string. If RESULT-TYPE equals 'value then return the value of the
292 last statement in BODY, as elisp."
293 (case result-type
294 (value
295 (let ((tmp-file (org-babel-temp-file "R-")))
296 (org-babel-eval org-babel-R-command
297 (format org-babel-R-write-object-command
298 (if row-names-p "TRUE" "FALSE")
299 (if column-names-p
300 (if row-names-p "NA" "TRUE")
301 "FALSE")
302 (format "{function ()\n{\n%s\n}}()" body)
303 (org-babel-process-file-name tmp-file 'noquote)))
304 (org-babel-R-process-value-result
305 (if (or (member "scalar" result-params)
306 (member "verbatim" result-params))
307 (with-temp-buffer
308 (insert-file-contents tmp-file)
309 (buffer-string))
310 (org-babel-import-elisp-from-file tmp-file '(16)))
311 column-names-p)))
312 (output (org-babel-eval org-babel-R-command body))))
314 (defun org-babel-R-evaluate-session
315 (session body result-type result-params column-names-p row-names-p)
316 "Evaluate BODY in SESSION.
317 If RESULT-TYPE equals 'output then return standard output as a
318 string. If RESULT-TYPE equals 'value then return the value of the
319 last statement in BODY, as elisp."
320 (case result-type
321 (value
322 (with-temp-buffer
323 (insert (org-babel-chomp body))
324 (let ((ess-local-process-name
325 (process-name (get-buffer-process session)))
326 (ess-eval-visibly-p nil))
327 (ess-eval-buffer nil)))
328 (let ((tmp-file (org-babel-temp-file "R-")))
329 (org-babel-comint-eval-invisibly-and-wait-for-file
330 session tmp-file
331 (format org-babel-R-write-object-command
332 (if row-names-p "TRUE" "FALSE")
333 (if column-names-p
334 (if row-names-p "NA" "TRUE")
335 "FALSE")
336 ".Last.value" (org-babel-process-file-name tmp-file 'noquote)))
337 (org-babel-R-process-value-result
338 (if (or (member "scalar" result-params)
339 (member "verbatim" result-params))
340 (with-temp-buffer
341 (insert-file-contents tmp-file)
342 (buffer-string))
343 (org-babel-import-elisp-from-file tmp-file '(16)))
344 column-names-p)))
345 (output
346 (mapconcat
347 #'org-babel-chomp
348 (butlast
349 (delq nil
350 (mapcar
351 (lambda (line) (when (> (length line) 0) line))
352 (mapcar
353 (lambda (line) ;; cleanup extra prompts left in output
354 (if (string-match
355 "^\\([ ]*[>+\\.][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line)
356 (substring line (match-end 1))
357 line))
358 (org-babel-comint-with-output (session org-babel-R-eoe-output)
359 (insert (mapconcat #'org-babel-chomp
360 (list body org-babel-R-eoe-indicator)
361 "\n"))
362 (inferior-ess-send-input)))))) "\n"))))
364 (defun org-babel-R-process-value-result (result column-names-p)
365 "R-specific processing of return value.
366 Insert hline if column names in output have been requested."
367 (if column-names-p
368 (cons (car result) (cons 'hline (cdr result)))
369 result))
371 (provide 'ob-R)
375 ;;; ob-R.el ends here