org-babel-R.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. (defvar org-babel-R-buffer "org-babel-R"
  28. "Holds the buffer for the current R process")
  29. (defun org-babel-R-initiate-R-buffer ()
  30. "If there is not a current R process then create one."
  31. (unless (org-babel-comint-buffer-livep org-babel-R-buffer)
  32. (save-window-excursion (R) (setf org-babel-R-buffer (current-buffer)))))
  33. (defun org-babel-execute:R (body params)
  34. "Execute a block of R code with org-babel. This function is
  35. called by `org-babel-execute-src-block'."
  36. (message "executing R source code block...")
  37. (save-window-excursion
  38. (let ((vars (org-babel-ref-variables params))
  39. (results-params (split-string (or (cdr (assoc :results params)) "")))
  40. results)
  41. ;; (message (format "%S" results-params))
  42. (org-babel-R-initiate-R-buffer)
  43. (mapc (lambda (pair) (org-babel-R-assign-elisp (car pair) (cdr pair))) vars)
  44. (cond
  45. ((member "script" results-params) ;; collect all output
  46. (let ((tmp-file (make-temp-file "org-babel-R-script-output")))
  47. (org-babel-comint-input-command org-babel-R-buffer (format "sink(%S)" tmp-file))
  48. (org-babel-comint-input-command org-babel-R-buffer body)
  49. (org-babel-comint-input-command org-babel-R-buffer "sink()")
  50. (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
  51. ((member "last" results-params) ;; the value of the last statement
  52. (org-babel-comint-input-command org-babel-R-buffer body)
  53. (org-babel-R-last-value-as-elisp))))))
  54. (defun org-babel-R-quote-tsv-field (s)
  55. "Quote field S for export to R."
  56. (if (stringp s)
  57. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  58. (format "%S" s)))
  59. (defun org-babel-R-assign-elisp (name value)
  60. "Read the elisp VALUE into a variable named NAME in the current
  61. R process in `org-babel-R-buffer'."
  62. (unless org-babel-R-buffer (error "No active R buffer"))
  63. (org-babel-comint-input-command
  64. org-babel-R-buffer
  65. (if (listp value)
  66. (let ((transition-file (make-temp-file "org-babel-R-import")))
  67. ;; ensure VALUE has an orgtbl structure (depth of at least 2)
  68. (unless (listp (car value)) (setq value (list value)))
  69. (with-temp-file transition-file
  70. (insert (orgtbl-to-tsv value '(:fmt org-babel-R-quote-tsv-field)))
  71. (insert "\n"))
  72. (format "%s <- read.table(\"%s\", header=FALSE, sep=\"\\t\", as.is=TRUE)"
  73. name transition-file))
  74. (format "%s <- %s" name (org-babel-R-quote-tsv-field value)))))
  75. (defun org-babel-R-last-value-as-elisp ()
  76. "Return the last value returned by R as Emacs lisp."
  77. (let ((tmp-file (make-temp-file "org-babel-R")) result)
  78. (org-babel-comint-input-command
  79. org-babel-R-buffer
  80. (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)"
  81. tmp-file))
  82. (org-babel-import-elisp-from-file tmp-file)))
  83. (defun org-babel-R-read (cell)
  84. "Strip nested \"s from around strings in exported R values."
  85. (org-babel-read (or (and (stringp cell)
  86. (string-match "\\\"\\(.+\\)\\\"" cell)
  87. (match-string 1 cell))
  88. cell)))
  89. (provide 'org-babel-R)
  90. ;;; org-babel-R.el ends here