org-babel-R.el 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ;;; org-babel-R.el --- org-babel functions for R code evaluation
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, R, statistics
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating R code
  24. ;;; Code:
  25. (require 'org-babel)
  26. (org-babel-add-interpreter "R")
  27. (add-to-list 'org-babel-tangle-langs '("R" "r"))
  28. (defun org-babel-execute:R (body params)
  29. "Execute a block of R code with org-babel. This function is
  30. called by `org-babel-execute-src-block'."
  31. (message "executing R source code block...")
  32. (save-window-excursion
  33. (let* ((vars (org-babel-ref-variables params))
  34. (full-body (concat
  35. (mapconcat ;; define any variables
  36. (lambda (pair)
  37. (org-babel-R-assign-elisp (car pair) (cdr pair)))
  38. vars "\n") "\n" body "\n"))
  39. (result-params (split-string (or (cdr (assoc :results params)) "")))
  40. (result-type (cond ((member "output" result-params) 'output)
  41. ((member "value" result-params) 'value)
  42. (t 'value)))
  43. (session (org-babel-R-initiate-session (cdr (assoc :session params))))
  44. results)
  45. ;; ;;; debugging statements
  46. ;; (message (format "result-type=%S" result-type))
  47. ;; (message (format "body=%S" body))
  48. ;; (message (format "session=%S" session))
  49. ;; (message (format "result-params=%S" result-params))
  50. ;; evaluate body and convert the results to ruby
  51. (setq results (org-babel-R-evaluate session full-body result-type))
  52. (setq results (if (member "scalar" result-params)
  53. results
  54. (let ((tmp-file (make-temp-file "org-babel-R")))
  55. (with-temp-file tmp-file (insert results))
  56. (org-babel-import-elisp-from-file tmp-file))))
  57. (if (and (member "vector" result-params) (not (listp results)))
  58. (list (list results))
  59. results))))
  60. (defun org-babel-prep-session:R (session params)
  61. "Prepare SESSION according to the header arguments specified in PARAMS."
  62. (let* ((session (org-babel-R-initiate-session session))
  63. (vars (org-babel-ref-variables params)))
  64. (mapc (lambda (pair) (org-babel-R-assign-elisp session (car pair) (cdr pair))) vars)))
  65. ;; helper functions
  66. (defun org-babel-R-quote-tsv-field (s)
  67. "Quote field S for export to R."
  68. (if (stringp s)
  69. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  70. (format "%S" s)))
  71. (defun org-babel-R-assign-elisp (name value)
  72. "Read the elisp VALUE into a variable named NAME."
  73. (if (listp value)
  74. (let ((transition-file (make-temp-file "org-babel-R-import")))
  75. ;; ensure VALUE has an orgtbl structure (depth of at least 2)
  76. (unless (listp (car value)) (setq value (list value)))
  77. (with-temp-file transition-file
  78. (insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
  79. (insert "\n"))
  80. (format "%s <- read.table(\"%s\", header=FALSE, sep=\"\\t\", as.is=TRUE)"
  81. name transition-file))
  82. (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
  83. (defun org-babel-R-initiate-session (session)
  84. "If there is not a current R process then create one."
  85. (unless (string= session "none")
  86. (setq session (or session "*R*"))
  87. (if (org-babel-comint-buffer-livep session)
  88. session
  89. (save-window-excursion (R) (rename-buffer (if (bufferp session) (buffer-name session)
  90. (if (stringp session) session (buffer-name)))) (current-buffer)))))
  91. (defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
  92. (defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
  93. (defvar org-babel-R-wrapper-method "main <- function ()\n{\n%s\n}
  94. write.table(main(), file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)")
  95. (defun org-babel-R-evaluate (buffer body result-type)
  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."
  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. ;; (message "R --slave --no-save < '%s' > '%s'" in-tmp-file out-tmp-file)
  108. (shell-command-to-string (format "R --slave --no-save < '%s' > '%s'" in-tmp-file out-tmp-file)))
  109. (value
  110. (with-temp-file in-tmp-file
  111. (insert (format org-babel-R-wrapper-method body out-tmp-file)))
  112. ;; (message "R --no-save < '%s'" in-tmp-file)
  113. (shell-command (format "R --no-save < '%s'" in-tmp-file))))
  114. (with-temp-buffer (insert-file-contents out-tmp-file) (buffer-string)))
  115. ;; comint session evaluation
  116. (org-babel-comint-in-buffer buffer
  117. (let* ((tmp-file (make-temp-file "org-babel-R"))
  118. (last-value-eval
  119. (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)"
  120. tmp-file))
  121. (full-body (mapconcat #'org-babel-chomp (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
  122. (raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil
  123. (insert full-body) (inferior-ess-send-input)))
  124. (results (let ((broke nil))
  125. (delete nil (mapcar (lambda (el)
  126. (if (or broke
  127. (and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
  128. nil
  129. (if (= (length el) 0)
  130. nil
  131. (if (string-match comint-prompt-regexp el)
  132. (substring el (match-end 0))
  133. el))))
  134. (mapcar #'org-babel-trim raw))))))
  135. (case result-type
  136. (output (org-babel-trim (mapconcat #'identity results "\n")))
  137. (value (org-babel-trim (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
  138. (t (reverse results)))))))
  139. (provide 'org-babel-R)
  140. ;;; org-babel-R.el ends here