org-babel-R.el 5.9 KB

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