org-babel-R.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. (result-params (split-string (or (cdr (assoc :results params)) "")))
  34. (result-type (cond ((member "output" result-params) 'output)
  35. ((member "value" result-params) 'value)
  36. (t 'value)))
  37. (session (org-babel-R-initiate-session ;; (cdr (assoc :session params))
  38. (get-buffer "*R*")))
  39. results)
  40. ;; assign variables
  41. (mapc (lambda (pair) (org-babel-R-assign-elisp session (car pair) (cdr pair))) vars)
  42. ;; ;;; debugging statements
  43. ;; (message (format "result-type=%S" result-type))
  44. ;; (message (format "body=%S" body))
  45. ;; (message (format "session=%S" session))
  46. ;; (message (format "result-params=%S" result-params))
  47. ;; evaluate body and convert the results to ruby
  48. (setq results (org-babel-R-evaluate session body result-type))
  49. (setq results (if (member "scalar" result-params)
  50. results
  51. (let ((tmp-file (make-temp-file "org-babel-R")))
  52. (with-temp-file tmp-file (insert results))
  53. (org-babel-import-elisp-from-file tmp-file))))
  54. (if (and (member "vector" result-params) (not (listp results)))
  55. (list (list results))
  56. results))))
  57. (defun org-babel-R-quote-tsv-field (s)
  58. "Quote field S for export to R."
  59. (if (stringp s)
  60. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  61. (format "%S" s)))
  62. (defun org-babel-R-assign-elisp (session name value)
  63. "Read the elisp VALUE into a variable named NAME in the current
  64. R process in `org-babel-R-buffer'."
  65. (unless session (error "No active R buffer"))
  66. (org-babel-comint-input-command
  67. session
  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=FALSE, sep=\"\\t\", as.is=TRUE)"
  76. name transition-file))
  77. (format "%s <- %s" name (org-babel-R-quote-tsv-field value)))))
  78. ;; functions for comint evaluation
  79. (defun org-babel-R-initiate-session (session)
  80. "If there is not a current R process then create one."
  81. (if (org-babel-comint-buffer-livep session)
  82. session
  83. (save-window-excursion (R) (current-buffer))))
  84. (defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
  85. (defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
  86. (defun org-babel-R-evaluate (buffer body result-type)
  87. "Pass BODY to the R process in BUFFER. If RESULT-TYPE equals
  88. 'output then return a list of the outputs of the statements in
  89. BODY, if RESULT-TYPE equals 'value then return the value of the
  90. last statement in BODY."
  91. (org-babel-comint-in-buffer buffer
  92. (let* ((string-buffer "")
  93. (tmp-file (make-temp-file "org-babel-R"))
  94. (last-value-eval
  95. (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)"
  96. tmp-file))
  97. (full-body (mapconcat #'org-babel-chomp (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
  98. results)
  99. (flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
  100. ;; setup filter
  101. (add-hook 'comint-output-filter-functions 'my-filt)
  102. ;; pass FULL-BODY to process
  103. (goto-char (process-mark (get-buffer-process buffer)))
  104. (insert full-body)
  105. (comint-send-input)
  106. ;; wait for end-of-evaluation indicator
  107. (while (progn
  108. (goto-char comint-last-input-end)
  109. (not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
  110. (re-search-forward (regexp-quote org-babel-R-eoe-output) nil t)))))
  111. (accept-process-output (get-buffer-process buffer)))
  112. ;; remove filter
  113. (remove-hook 'comint-output-filter-functions 'my-filt))
  114. ;; (message (format "raw-results=%S" string-buffer)) ;; debugging
  115. ;; ;; split results
  116. ;; split results with `comint-prompt-regexp'
  117. (setq results (let ((broke nil))
  118. (delete nil (mapcar (lambda (el)
  119. (if (or broke
  120. (and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
  121. nil
  122. (if (= (length el) 0)
  123. nil
  124. (if (string-match comint-prompt-regexp el)
  125. (substring el (match-end 0))
  126. el))))
  127. (mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp))))))
  128. (case result-type
  129. (output (org-babel-trim (mapconcat #'identity results "\n")))
  130. (value (org-babel-trim (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
  131. (t (reverse results))))))
  132. (provide 'org-babel-R)
  133. ;;; org-babel-R.el ends here