1 ;;; org-babel-R.el --- org-babel functions for R code evaluation
3 ;; Copyright (C) 2009 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research, R, statistics
7 ;; Homepage: http://orgmode.org
12 ;; This program 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, or (at your option)
17 ;; This program 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Org-Babel support for evaluating R code
34 (org-babel-add-interpreter "R")
36 (add-to-list 'org-babel-tangle-langs
'("R" "r"))
38 (defun org-babel-execute:R
(body params
)
39 "Execute a block of R code with org-babel. This function is
40 called by `org-babel-execute-src-block' via multiple-value-bind."
41 (message "executing R source code block...")
42 (save-window-excursion
43 (let ((full-body (concat
44 (mapconcat ;; define any variables
46 (org-babel-R-assign-elisp (car pair
) (cdr pair
)))
47 vars
"\n") "\n" body
"\n"))
48 (session (org-babel-R-initiate-session session
))
49 (colnames (cdr (assoc :colnames params
))))
50 (org-babel-R-evaluate session full-body result-type colnames
))))
52 (defun org-babel-prep-session:R
(session params
)
53 "Prepare SESSION according to the header arguments specified in PARAMS."
54 (let* ((session (org-babel-R-initiate-session session
))
55 (vars (org-babel-ref-variables params
)))
56 (mapc (lambda (pair) (org-babel-R-assign-elisp session
(car pair
) (cdr pair
))) vars
)))
60 (defun org-babel-R-quote-tsv-field (s)
61 "Quote field S for export to R."
63 (concat "\"" (mapconcat 'identity
(split-string s
"\"") "\"\"") "\"")
66 (defun org-babel-R-assign-elisp (name value
)
67 "Read the elisp VALUE into a variable named NAME."
69 (let ((transition-file (make-temp-file "org-babel-R-import")))
70 ;; ensure VALUE has an orgtbl structure (depth of at least 2)
71 (unless (listp (car value
)) (setq value
(list value
)))
72 (with-temp-file transition-file
73 (insert (orgtbl-to-tsv value
'(:fmt org-babel-R-quote-tsv-field
)))
75 (format "%s <- read.table(\"%s\", header=%s, sep=\"\\t\", as.is=TRUE)"
76 name transition-file
(if (eq (second value
) 'hline
) "TRUE" "FALSE")))
77 (format "%s <- %s" name
(org-babel-R-quote-tsv-field value
))))
79 (defun org-babel-R-initiate-session (session)
80 "If there is not a current R process then create one."
81 (unless (string= session
"none")
82 (setq session
(or session
"*R*"))
83 (if (org-babel-comint-buffer-livep session
)
85 (save-window-excursion
87 (rename-buffer (if (bufferp session
) (buffer-name session
)
88 (if (stringp session
) session
(buffer-name)))) (current-buffer)))))
90 (defvar org-babel-R-eoe-indicator
"'org_babel_R_eoe'")
91 (defvar org-babel-R-eoe-output
"[1] \"org_babel_R_eoe\"")
92 (defvar org-babel-R-wrapper-method
"main <- function ()\n{\n%s\n}
93 write.table(main(), file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=%s, quote=FALSE)")
95 (defun org-babel-R-evaluate (buffer body result-type colnames
)
96 "Pass BODY to the R process in BUFFER. If RESULT-TYPE equals
97 'output then return a list of the outputs of the statements in
98 BODY, if RESULT-TYPE equals 'value then return the value of the
99 last statement in BODY, as elisp."
101 ;; external process evaluation
102 (let ((in-tmp-file (make-temp-file "R-in-functional-results"))
103 (out-tmp-file (make-temp-file "R-out-functional-results")))
106 (with-temp-file in-tmp-file
(insert body
))
107 (shell-command-to-string (format "R --slave --no-save < '%s' > '%s'"
108 in-tmp-file out-tmp-file
))
109 (with-temp-buffer (insert-file-contents out-tmp-file
) (buffer-string)))
111 (with-temp-file in-tmp-file
112 (insert (format org-babel-R-wrapper-method
113 body out-tmp-file
(if colnames
"TRUE" "FALSE"))))
114 (shell-command (format "R --no-save < '%s'" in-tmp-file
))
115 (org-babel-import-elisp-from-file out-tmp-file colnames
))))
116 ;; comint session evaluation
117 (org-babel-comint-in-buffer buffer
118 (let* ((tmp-file (make-temp-file "org-babel-R"))
120 (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=%s, quote=FALSE)" tmp-file
(if colnames
"TRUE" "FALSE")))
121 (full-body (mapconcat #'org-babel-chomp
122 (list body last-value-eval org-babel-R-eoe-indicator
) "\n"))
123 (raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil
124 (insert full-body
) (inferior-ess-send-input)))
131 (and (string-match (regexp-quote org-babel-R-eoe-output
)
134 (if (= (length el
) 0)
136 (if (string-match comint-prompt-regexp el
)
137 (substring el
(match-end 0))
139 (mapcar #'org-babel-trim raw
))))))
141 (output (org-babel-chomp (mapconcat #'identity results
"\n")))
142 (value (org-babel-import-elisp-from-file tmp-file colnames
)))))))
145 (provide 'org-babel-R
)
146 ;;; org-babel-R.el ends here