litorgy-R.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ;;; litorgy-R.el --- litorgy 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. ;; Litorgy support for evaluating R code
  24. ;;; Code:
  25. (require 'litorgy)
  26. (litorgy-add-interpreter "R")
  27. (defun litorgy-execute:R (body params)
  28. "Execute a block of R code with litorgy. This function is
  29. called by `litorgy-execute-src-block'."
  30. (message "executing R source code block...")
  31. (save-window-excursion
  32. (let ((vars (litorgy-reference-variables params))
  33. results)
  34. (litorgy-R-initiate-R-buffer)
  35. (litorgy-R-command-to-string body))))
  36. ;; functions for evaluation of R code
  37. (defvar litorgy-R-buffer nil
  38. "Holds the buffer for the current R process")
  39. (defun litorgy-R-initiate-R-buffer ()
  40. "If there is not a current R process then create one."
  41. (unless (and (buffer-live-p litorgy-R-buffer) (get-buffer litorgy-R-buffer))
  42. (save-excursion
  43. (R)
  44. (setf litorgy-R-buffer (current-buffer))
  45. (litorgy-R-wait-for-output)
  46. (litorgy-R-input-command ""))))
  47. (defun litorgy-R-command-to-string (command)
  48. "Send a command to R, and return the results as a string."
  49. (litorgy-R-input-command command)
  50. (litorgy-R-last-output))
  51. (defun litorgy-R-input-command (command)
  52. "Pass COMMAND to the R process running in `litorgy-R-buffer'."
  53. (save-excursion
  54. (save-match-data
  55. (set-buffer litorgy-R-buffer)
  56. (goto-char (process-mark (get-buffer-process (current-buffer))))
  57. (insert command)
  58. (comint-send-input)
  59. (litorgy-R-wait-for-output))))
  60. (defun litorgy-R-wait-for-output ()
  61. "Wait until output arrives"
  62. (save-excursion
  63. (save-match-data
  64. (set-buffer litorgy-R-buffer)
  65. (while (progn
  66. (goto-char comint-last-input-end)
  67. (not (re-search-forward comint-prompt-regexp nil t)))
  68. (accept-process-output (get-buffer-process (current-buffer)))))))
  69. (defun litorgy-R-last-output ()
  70. "Return the last R output as a string"
  71. (save-excursion
  72. (save-match-data
  73. (set-buffer litorgy-R-buffer)
  74. (goto-char (process-mark (get-buffer-process (current-buffer))))
  75. (forward-line 0)
  76. (let ((raw (buffer-substring comint-last-input-end (- (point) 1)))
  77. output output-flag)
  78. (mapconcat
  79. (lambda (el)
  80. (if (stringp el)
  81. (format "%s" el)
  82. (format "%S" el)))
  83. (delq nil
  84. (mapcar
  85. (lambda (line)
  86. (unless (string-match "^>" line)
  87. (and (string-match "\\[[[:digit:]]+\\] *\\(.*\\)$" line)
  88. (match-string 1 line))))
  89. ;; drop first, because it's the last line of input
  90. (cdr (split-string raw "[\n\r]")))) "\n")))))
  91. (defun litorgy-R-table-or-results (results)
  92. "If the results look like a matrix, then convert them into an
  93. Emacs-lisp table otherwise return the results as a string."
  94. ;; TODO: these simple assumptions will probably need some tweaking
  95. (when (string-match "[ \f\t\n\r\v]+" results)
  96. (concat "(" (mapconcat #'litorgy-R-tale-or-results
  97. (split-string results) " ") ")"))
  98. results)
  99. (provide 'litorgy-R)
  100. ;;; litorgy-R.el ends here