Adding support for column names (header line) when using R.
[rgr-org-mode.git] / lisp / langs / org-babel-R.el
blob5ec3efce414acaaa735a4ec823e2d4b06eab97af
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
8 ;; Version: 0.01
10 ;;; License:
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)
15 ;; any later version.
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.
27 ;;; Commentary:
29 ;; Org-Babel support for evaluating R code
31 ;;; Code:
32 (require 'org-babel)
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
45 (lambda (pair)
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)))
58 ;; helper functions
60 (defun org-babel-R-quote-tsv-field (s)
61 "Quote field S for export to R."
62 (if (stringp s)
63 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
64 (format "%S" s)))
66 (defun org-babel-R-assign-elisp (name value)
67 "Read the elisp VALUE into a variable named NAME."
68 (if (listp value)
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)))
74 (insert "\n"))
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)
84 session
85 (save-window-excursion
86 (R)
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."
100 (if (not session)
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")))
104 (case result-type
105 (output
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)))
110 (value
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"))
119 (last-value-eval
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)))
125 (results
126 (let ((broke nil))
127 (delete
129 (mapcar (lambda (el)
130 (if (or broke
131 (and (string-match (regexp-quote org-babel-R-eoe-output)
132 el) (setq broke t)))
134 (if (= (length el) 0)
136 (if (string-match comint-prompt-regexp el)
137 (substring el (match-end 0))
138 el))))
139 (mapcar #'org-babel-trim raw))))))
140 (case result-type
141 (output (org-babel-trim (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