litorgy-R.el 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ;;; litorgy-R.el --- litorgy functions for R code evaluation
  2. ;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank
  3. ;; Author: Eric Schulte, Dan Davison, Austin F. Frank
  4. ;; Keywords: literate programming, reproducible research
  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. (let (results)
  31. (message "executing R code block...")
  32. (litorgy-initiate-R-buffer)
  33. (mapc (lambda (line) (litorgy-R-input-command line)) (butlast (split-string body "[\r\n]")))
  34. (litorgy-R-last-output)))
  35. ;; Maybe the following be replaced with a method using `ess-execute',
  36. ;; I went with the following functions because I wrote them and they
  37. ;; are what I know
  38. ;;
  39. ;; (not the best reasons for making design decisions)
  40. (defvar litorgy-R-buffer nil
  41. "Holds the buffer for the current R process")
  42. (defun litorgy-initiate-R-buffer ()
  43. "If there is not a current R process then create one."
  44. (unless (and (buffer-live-p litorgy-R-buffer) (get-buffer litorgy-R-buffer))
  45. (save-excursion
  46. (R)
  47. (setf litorgy-R-buffer (current-buffer))
  48. (litorgy-R-wait-for-output)
  49. (litorgy-R-input-command ""))))
  50. (defun litorgy-R-command-to-string (command)
  51. "Send a command to R, and return the results as a string."
  52. (litorgy-R-input-command command)
  53. (litorgy-R-last-output))
  54. (defun litorgy-R-input-command (command)
  55. "Pass COMMAND to the R process running in `litorgy-R-buffer'."
  56. (save-excursion
  57. (save-match-data
  58. (set-buffer litorgy-R-buffer)
  59. (goto-char (process-mark (get-buffer-process (current-buffer))))
  60. (insert command)
  61. (comint-send-input)
  62. (litorgy-R-wait-for-output))))
  63. (defun litorgy-R-wait-for-output ()
  64. "Wait until output arrives"
  65. (save-excursion
  66. (save-match-data
  67. (set-buffer litorgy-R-buffer)
  68. (while (progn
  69. (goto-char comint-last-input-end)
  70. (not (re-search-forward comint-prompt-regexp nil t)))
  71. (accept-process-output (get-buffer-process (current-buffer)))))))
  72. (defun litorgy-R-last-output ()
  73. "Return the last R output as a string"
  74. (save-excursion
  75. (save-match-data
  76. (set-buffer litorgy-R-buffer)
  77. (goto-char (process-mark (get-buffer-process (current-buffer))))
  78. (forward-line 0)
  79. (let ((raw (buffer-substring comint-last-input-end (- (point) 1))))
  80. (if (string-match "\n" raw)
  81. raw
  82. (and (string-match "\\[[[:digit:]+]\\] *\\(.*\\)$" raw)
  83. (message raw)
  84. (message (match-string 1 raw))
  85. (match-string 1 raw)))))))
  86. (provide 'litorgy-R)
  87. ;;; litorgy-R.el ends here