org-babel-R.el 6.1 KB

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