org-babel-R.el 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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-prep-session:R (session params)
  57. "Prepare SESSION according to the header arguments specified in PARAMS."
  58. (let* ((session (org-babel-R-initiate-session session))
  59. (vars (org-babel-ref-variables params)))
  60. (mapc (lambda (pair) (org-babel-R-assign-elisp session (car pair) (cdr pair))) vars)))
  61. ;; helper functions
  62. (defun org-babel-R-quote-tsv-field (s)
  63. "Quote field S for export to R."
  64. (if (stringp s)
  65. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  66. (format "%S" s)))
  67. (defun org-babel-R-assign-elisp (session name value)
  68. "Read the elisp VALUE into a variable named NAME in the current
  69. R process in `org-babel-R-buffer'."
  70. (unless session (error "No active R buffer"))
  71. (org-babel-comint-input-command
  72. session
  73. (if (listp value)
  74. (let ((transition-file (make-temp-file "org-babel-R-import")))
  75. ;; ensure VALUE has an orgtbl structure (depth of at least 2)
  76. (unless (listp (car value)) (setq value (list value)))
  77. (with-temp-file transition-file
  78. (insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
  79. (insert "\n"))
  80. (format "%s <- read.table(\"%s\", header=FALSE, sep=\"\\t\", as.is=TRUE)"
  81. name transition-file))
  82. (format "%s <- %s" name (org-babel-R-quote-tsv-field value)))))
  83. (defun org-babel-R-initiate-session (session)
  84. "If there is not a current R process then create one."
  85. (setq session (or session "*R*"))
  86. (if (org-babel-comint-buffer-livep session)
  87. session
  88. (save-window-excursion (R) (rename-buffer (if (bufferp session) (buffer-name session)
  89. (if (stringp session) session (buffer-name)))) (current-buffer))))
  90. (defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
  91. (defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
  92. (defun org-babel-R-evaluate (buffer body result-type)
  93. "Pass BODY to the R process in BUFFER. If RESULT-TYPE equals
  94. 'output then return a list of the outputs of the statements in
  95. BODY, if RESULT-TYPE equals 'value then return the value of the
  96. last statement in BODY."
  97. (org-babel-comint-in-buffer buffer
  98. (let* ((tmp-file (make-temp-file "org-babel-R"))
  99. (last-value-eval
  100. (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)"
  101. tmp-file))
  102. (full-body (mapconcat #'org-babel-chomp (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
  103. (raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil
  104. (insert full-body) (inferior-ess-send-input)))
  105. (results (let ((broke nil))
  106. (delete nil (mapcar (lambda (el)
  107. (if (or broke
  108. (and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
  109. nil
  110. (if (= (length el) 0)
  111. nil
  112. (if (string-match comint-prompt-regexp el)
  113. (substring el (match-end 0))
  114. el))))
  115. (mapcar #'org-babel-trim raw))))))
  116. (case result-type
  117. (output (org-babel-trim (mapconcat #'identity results "\n")))
  118. (value (org-babel-trim (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
  119. (t (reverse results))))))
  120. (provide 'org-babel-R)
  121. ;;; org-babel-R.el ends here