applying org-babel-comint-with-output macro to org-babel-R.el

This commit is contained in:
Eric Schulte 2009-06-13 17:34:51 -07:00
parent f4a1193a6e
commit e39b5e848e
1 changed files with 13 additions and 31 deletions

View File

@ -105,42 +105,24 @@ R process in `org-babel-R-buffer'."
BODY, if RESULT-TYPE equals 'value then return the value of the
last statement in BODY."
(org-babel-comint-in-buffer buffer
(let* ((string-buffer "")
(tmp-file (make-temp-file "org-babel-R"))
(let* ((tmp-file (make-temp-file "org-babel-R"))
(last-value-eval
(format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)"
tmp-file))
(full-body (mapconcat #'org-babel-chomp (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
results)
(flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
;; setup filter
(add-hook 'comint-output-filter-functions 'my-filt)
;; pass FULL-BODY to process
(goto-char (process-mark (get-buffer-process buffer)))
(insert full-body)
(comint-send-input)
;; wait for end-of-evaluation indicator
(while (progn
(goto-char comint-last-input-end)
(not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
(re-search-forward (regexp-quote org-babel-R-eoe-output) nil t)))))
(accept-process-output (get-buffer-process buffer)))
;; remove filter
(remove-hook 'comint-output-filter-functions 'my-filt))
;; (message (format "raw-results=%S" string-buffer)) ;; debugging
;; ;; split results
;; split results with `comint-prompt-regexp'
(setq results (let ((broke nil))
(raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil
(insert full-body) (comint-send-input)))
(results (let ((broke nil))
(delete nil (mapcar (lambda (el)
(if (or broke
(and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
nil
(if (= (length el) 0)
nil
(if (string-match comint-prompt-regexp el)
(substring el (match-end 0))
el))))
(mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp))))))
(if (or broke
(and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
nil
(if (= (length el) 0)
nil
(if (string-match comint-prompt-regexp el)
(substring el (match-end 0))
el))))
(mapcar #'org-babel-trim raw))))))
(case result-type
(output (org-babel-trim (mapconcat #'identity results "\n")))
(value (org-babel-trim (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))