org-babel-R.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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' via multiple-value-bind."
  31. (message "executing R source code block...")
  32. (save-window-excursion
  33. (let ((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. (session (org-babel-R-initiate-session session))
  39. (column-names-p (cdr (assoc :colnames params))))
  40. (org-babel-R-evaluate session full-body result-type column-names-p))))
  41. (defun org-babel-prep-session:R (session params)
  42. "Prepare SESSION according to the header arguments specified in PARAMS."
  43. (let* ((session (org-babel-R-initiate-session session))
  44. (vars (org-babel-ref-variables params)))
  45. (mapc (lambda (pair) (org-babel-R-assign-elisp session (car pair) (cdr pair))) vars)))
  46. ;; helper functions
  47. (defun org-babel-R-quote-tsv-field (s)
  48. "Quote field S for export to R."
  49. (if (stringp s)
  50. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  51. (format "%S" s)))
  52. (defun org-babel-R-assign-elisp (name value)
  53. "Read the elisp VALUE into a variable named NAME."
  54. (if (listp value)
  55. (let ((transition-file (make-temp-file "org-babel-R-import")))
  56. ;; ensure VALUE has an orgtbl structure (depth of at least 2)
  57. (unless (listp (car value)) (setq value (list value)))
  58. (with-temp-file transition-file
  59. (insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
  60. (insert "\n"))
  61. (format "%s <- read.table(\"%s\", header=%s, sep=\"\\t\", as.is=TRUE)"
  62. name transition-file (if (eq (second value) 'hline) "TRUE" "FALSE")))
  63. (format "%s <- %s" name (org-babel-R-quote-tsv-field value))))
  64. (defun org-babel-R-initiate-session (session)
  65. "If there is not a current R process then create one."
  66. (unless (string= session "none")
  67. (setq session (or session "*R*"))
  68. (if (org-babel-comint-buffer-livep session)
  69. session
  70. (save-window-excursion
  71. (R)
  72. (rename-buffer (if (bufferp session) (buffer-name session)
  73. (if (stringp session) session (buffer-name)))) (current-buffer)))))
  74. (defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
  75. (defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
  76. (defvar org-babel-R-wrapper-method "main <- function ()\n{\n%s\n}
  77. write.table(main(), file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=%s, quote=FALSE)")
  78. (defun org-babel-R-evaluate (buffer body result-type column-names-p)
  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, as elisp."
  83. (if (not session)
  84. ;; external process evaluation
  85. (let ((in-tmp-file (make-temp-file "R-in-functional-results"))
  86. (out-tmp-file (make-temp-file "R-out-functional-results")))
  87. (case result-type
  88. (output
  89. (with-temp-file in-tmp-file (insert body))
  90. (shell-command-to-string (format "R --slave --no-save < '%s' > '%s'"
  91. in-tmp-file out-tmp-file))
  92. (with-temp-buffer (insert-file-contents out-tmp-file) (buffer-string)))
  93. (value
  94. (with-temp-file in-tmp-file
  95. (insert (format org-babel-R-wrapper-method
  96. body out-tmp-file (if column-names-p "TRUE" "FALSE"))))
  97. (shell-command (format "R --no-save < '%s'" in-tmp-file))
  98. (org-babel-R-process-value-result
  99. (org-babel-import-elisp-from-file out-tmp-file) column-names-p))))
  100. ;; comint session evaluation
  101. (org-babel-comint-in-buffer buffer
  102. (let* ((tmp-file (make-temp-file "org-babel-R"))
  103. (full-body
  104. (case result-type
  105. (value
  106. (mapconcat #'org-babel-chomp (list body
  107. (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=%s, quote=FALSE)" tmp-file (if column-names-p "TRUE" "FALSE"))
  108. org-babel-R-eoe-indicator) "\n"))
  109. (output
  110. (mapconcat #'org-babel-chomp (list body org-babel-R-eoe-indicator) "\n"))))
  111. (raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil
  112. (insert full-body) (inferior-ess-send-input)))
  113. broke results)
  114. (case result-type
  115. (value (org-babel-R-process-value-result
  116. (org-babel-import-elisp-from-file tmp-file) column-names-p))
  117. (output
  118. (flet ((extractor
  119. (el)
  120. (if (or broke
  121. (and (string-match (regexp-quote org-babel-R-eoe-output) el)
  122. (setq broke t)))
  123. nil
  124. (if (= (length el) 0)
  125. nil
  126. (if (string-match comint-prompt-regexp el)
  127. (substring el (match-end 0))
  128. el)))))
  129. (mapconcat
  130. #'identity
  131. (delete nil (mapcar #'extractor (mapcar #'org-babel-chomp raw))) "\n"))))))))
  132. (defun org-babel-R-process-value-result (result column-names-p)
  133. "R-specific processing of return value prior to return to org-babel.
  134. Currently, insert hline if column names in output have been requested."
  135. (if column-names-p
  136. (cons (car result) (cons 'hline (cdr result)))
  137. result))
  138. (provide 'org-babel-R)
  139. ;;; org-babel-R.el ends here