org-babel-R.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. results)
  34. (org-babel-R-initiate-R-buffer)
  35. (mapc (lambda (pair) (org-babel-R-assign-elisp (car pair) (cdr pair))) vars)
  36. (org-babel-R-input-command body)
  37. (org-babel-R-last-value-as-elisp))))
  38. (defun org-babel-R-quote-tsv-field (s)
  39. "Quote field S for export to R."
  40. (if (stringp s)
  41. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  42. (format "%S" s)))
  43. (defun org-babel-R-assign-elisp (name value)
  44. "Read the elisp VALUE into a variable named NAME in the current
  45. R process in `org-babel-R-buffer'."
  46. (unless org-babel-R-buffer (error "No active R buffer"))
  47. (org-babel-R-input-command
  48. (if (listp value)
  49. (let ((transition-file (make-temp-file "org-babel-R-import")))
  50. ;; ensure VALUE has an orgtbl structure (depth of at least 2)
  51. (unless (listp (car value)) (setq value (list value)))
  52. (with-temp-file transition-file
  53. (insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
  54. (insert "\n"))
  55. (format "%s <- read.table(\"%s\", header=FALSE, sep=\"\\t\", as.is=TRUE)"
  56. name transition-file))
  57. (format "%s <- %s" name (org-babel-R-quote-tsv-field value)))))
  58. (defun org-babel-R-last-value-as-elisp ()
  59. "Return the last value returned by R as Emacs lisp."
  60. (let ((tmp-file (make-temp-file "org-babel-R")) result)
  61. (org-babel-R-input-command
  62. (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)"
  63. tmp-file))
  64. (with-temp-buffer
  65. (condition-case nil
  66. (progn
  67. (org-table-import tmp-file nil)
  68. (delete-file tmp-file)
  69. (setq result (mapcar (lambda (row)
  70. (mapcar #'org-babel-R-read row))
  71. (org-table-to-lisp))))
  72. (error nil))
  73. (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
  74. (if (consp (car result))
  75. (if (null (cdr (car result)))
  76. (caar result)
  77. result)
  78. (car result))
  79. result))))
  80. (defun org-babel-R-read (cell)
  81. "Strip nested \"s from around strings in exported R values."
  82. (org-babel-read (or (and (stringp cell)
  83. (string-match "\\\"\\(.+\\)\\\"" cell)
  84. (match-string 1 cell))
  85. cell)))
  86. ;; functions for evaluation of R code
  87. (defvar org-babel-R-buffer nil
  88. "Holds the buffer for the current R process")
  89. (defun org-babel-R-initiate-R-buffer ()
  90. "If there is not a current R process then create one."
  91. (unless (and (buffer-live-p org-babel-R-buffer) (get-buffer org-babel-R-buffer))
  92. (save-excursion
  93. (R)
  94. (setf org-babel-R-buffer (current-buffer))
  95. (org-babel-R-wait-for-output)
  96. (org-babel-R-input-command ""))))
  97. (defun org-babel-R-command-to-string (command)
  98. "Send a command to R, and return the results as a string."
  99. (org-babel-R-input-command command)
  100. (org-babel-R-last-output))
  101. (defun org-babel-R-input-command (command)
  102. "Pass COMMAND to the R process running in `org-babel-R-buffer'."
  103. (save-excursion
  104. (save-match-data
  105. (set-buffer org-babel-R-buffer)
  106. (goto-char (process-mark (get-buffer-process (current-buffer))))
  107. (insert command)
  108. (comint-send-input)
  109. (org-babel-R-wait-for-output))))
  110. (defun org-babel-R-wait-for-output ()
  111. "Wait until output arrives"
  112. (save-excursion
  113. (save-match-data
  114. (set-buffer org-babel-R-buffer)
  115. (while (progn
  116. (goto-char comint-last-input-end)
  117. (not (re-search-forward comint-prompt-regexp nil t)))
  118. (accept-process-output (get-buffer-process (current-buffer)))))))
  119. (defun org-babel-R-last-output ()
  120. "Return the last R output as a string"
  121. (save-excursion
  122. (save-match-data
  123. (set-buffer org-babel-R-buffer)
  124. (goto-char (process-mark (get-buffer-process (current-buffer))))
  125. (forward-line 0)
  126. (let ((raw (buffer-substring comint-last-input-end (- (point) 1)))
  127. output output-flag)
  128. (mapconcat
  129. (lambda (el)
  130. (if (stringp el)
  131. (format "%s" el)
  132. (format "%S" el)))
  133. (delq nil
  134. (mapcar
  135. (lambda (line)
  136. (unless (string-match "^>" line)
  137. (and (string-match "\\[[[:digit:]]+\\] *\\(.*\\)$" line)
  138. (match-string 1 line))))
  139. ;; drop first, because it's the last line of input
  140. (cdr (split-string raw "[\n\r]")))) "\n")))))
  141. (provide 'org-babel-R)
  142. ;;; org-babel-R.el ends here