litorgy-R.el 3.5 KB

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