org-babel-R.el 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. (defun org-babel-execute:R (body params)
  28. "Execute a block of R code with org-babel. This function is
  29. called by `org-babel-execute-src-block'."
  30. (message "executing R source code block...")
  31. (save-window-excursion
  32. (let* ((vars (org-babel-ref-variables params))
  33. (full-body (concat
  34. (mapconcat ;; define any variables
  35. (lambda (pair)
  36. (org-babel-R-assign-elisp (car pair) (cdr pair)))
  37. vars "\n") "\n" body "\n"))
  38. (result-params (split-string (or (cdr (assoc :results params)) "")))
  39. (result-type (cond ((member "output" result-params) 'output)
  40. ((member "value" result-params) 'value)
  41. (t 'value)))
  42. (session (org-babel-R-initiate-session (cdr (assoc :session params))))
  43. results)
  44. ;; ;;; debugging statements
  45. ;; (message (format "result-type=%S" result-type))
  46. ;; (message (format "body=%S" body))
  47. ;; (message (format "session=%S" session))
  48. ;; (message (format "result-params=%S" result-params))
  49. ;; evaluate body and convert the results to ruby
  50. (setq results (org-babel-R-evaluate session full-body result-type))
  51. (setq results (if (member "scalar" result-params)
  52. results
  53. (let ((tmp-file (make-temp-file "org-babel-R")))
  54. (with-temp-file tmp-file (insert results))
  55. (org-babel-import-elisp-from-file tmp-file))))
  56. (if (and (member "vector" result-params) (not (listp results)))
  57. (list (list results))
  58. results))))
  59. (defun org-babel-prep-session:R (session params)
  60. "Prepare SESSION according to the header arguments specified in PARAMS."
  61. (let* ((session (org-babel-R-initiate-session session))
  62. (vars (org-babel-ref-variables params)))
  63. (mapc (lambda (pair) (org-babel-R-assign-elisp session (car pair) (cdr pair))) vars)))
  64. ;; helper functions
  65. (defun org-babel-R-quote-tsv-field (s)
  66. "Quote field S for export to R."
  67. (if (stringp s)
  68. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  69. (format "%S" s)))
  70. (defun org-babel-R-assign-elisp (name value)
  71. "Read the elisp VALUE into a variable named NAME."
  72. (if (listp value)
  73. (let ((transition-file (make-temp-file "org-babel-R-import")))
  74. ;; ensure VALUE has an orgtbl structure (depth of at least 2)
  75. (unless (listp (car value)) (setq value (list value)))
  76. (with-temp-file transition-file
  77. (insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
  78. (insert "\n"))
  79. (format "%s <- read.table(\"%s\", header=FALSE, sep=\"\\t\", as.is=TRUE)"
  80. name transition-file))
  81. (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
  82. (defun org-babel-R-initiate-session (session)
  83. "If there is not a current R process then create one."
  84. (unless (string= session "none")
  85. (setq session (or session "*R*"))
  86. (if (org-babel-comint-buffer-livep session)
  87. session
  88. (save-window-excursion (R) (rename-buffer (if (bufferp session) (buffer-name session)
  89. (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=FALSE, quote=FALSE)")
  94. (defun org-babel-R-evaluate (buffer body result-type)
  95. "Pass BODY to the R process in BUFFER. If RESULT-TYPE equals
  96. 'output then return a list of the outputs of the statements in
  97. BODY, if RESULT-TYPE equals 'value then return the value of the
  98. last statement in BODY."
  99. (if (not session)
  100. ;; external process evaluation
  101. (let ((in-tmp-file (make-temp-file "R-in-functional-results"))
  102. (out-tmp-file (make-temp-file "R-out-functional-results")))
  103. (case result-type
  104. (output
  105. (with-temp-file in-tmp-file (insert body))
  106. ;; (message "R --slave --no-save < '%s' > '%s'" in-tmp-file out-tmp-file)
  107. (shell-command-to-string (format "R --slave --no-save < '%s' > '%s'" in-tmp-file out-tmp-file)))
  108. (value
  109. (with-temp-file in-tmp-file
  110. (insert (format org-babel-R-wrapper-method body out-tmp-file)))
  111. ;; (message "R --no-save < '%s'" in-tmp-file)
  112. (shell-command (format "R --no-save < '%s'" in-tmp-file))))
  113. (with-temp-buffer (insert-file-contents out-tmp-file) (buffer-string)))
  114. ;; comint session evaluation
  115. (org-babel-comint-in-buffer buffer
  116. (let* ((tmp-file (make-temp-file "org-babel-R"))
  117. (last-value-eval
  118. (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)"
  119. tmp-file))
  120. (full-body (mapconcat #'org-babel-chomp (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
  121. (raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil
  122. (insert full-body) (inferior-ess-send-input)))
  123. (results (let ((broke nil))
  124. (delete nil (mapcar (lambda (el)
  125. (if (or broke
  126. (and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
  127. nil
  128. (if (= (length el) 0)
  129. nil
  130. (if (string-match comint-prompt-regexp el)
  131. (substring el (match-end 0))
  132. el))))
  133. (mapcar #'org-babel-trim raw))))))
  134. (case result-type
  135. (output (org-babel-trim (mapconcat #'identity results "\n")))
  136. (value (org-babel-trim (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
  137. (t (reverse results)))))))
  138. (provide 'org-babel-R)
  139. ;;; org-babel-R.el ends here