applying org-babel-comint-with-output macro to org-babel-R.el
This commit is contained in:
parent
f4a1193a6e
commit
e39b5e848e
|
@ -105,42 +105,24 @@ R process in `org-babel-R-buffer'."
|
||||||
BODY, if RESULT-TYPE equals 'value then return the value of the
|
BODY, if RESULT-TYPE equals 'value then return the value of the
|
||||||
last statement in BODY."
|
last statement in BODY."
|
||||||
(org-babel-comint-in-buffer buffer
|
(org-babel-comint-in-buffer buffer
|
||||||
(let* ((string-buffer "")
|
(let* ((tmp-file (make-temp-file "org-babel-R"))
|
||||||
(tmp-file (make-temp-file "org-babel-R"))
|
|
||||||
(last-value-eval
|
(last-value-eval
|
||||||
(format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)"
|
(format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)"
|
||||||
tmp-file))
|
tmp-file))
|
||||||
(full-body (mapconcat #'org-babel-chomp (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
|
(full-body (mapconcat #'org-babel-chomp (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
|
||||||
results)
|
(raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil
|
||||||
(flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
|
(insert full-body) (comint-send-input)))
|
||||||
;; setup filter
|
(results (let ((broke nil))
|
||||||
(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))
|
|
||||||
(delete nil (mapcar (lambda (el)
|
(delete nil (mapcar (lambda (el)
|
||||||
(if (or broke
|
(if (or broke
|
||||||
(and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
|
(and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
|
||||||
nil
|
nil
|
||||||
(if (= (length el) 0)
|
(if (= (length el) 0)
|
||||||
nil
|
nil
|
||||||
(if (string-match comint-prompt-regexp el)
|
(if (string-match comint-prompt-regexp el)
|
||||||
(substring el (match-end 0))
|
(substring el (match-end 0))
|
||||||
el))))
|
el))))
|
||||||
(mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp))))))
|
(mapcar #'org-babel-trim raw))))))
|
||||||
(case result-type
|
(case result-type
|
||||||
(output (org-babel-trim (mapconcat #'identity results "\n")))
|
(output (org-babel-trim (mapconcat #'identity results "\n")))
|
||||||
(value (org-babel-trim (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
|
(value (org-babel-trim (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
|
||||||
|
|
Loading…
Reference in New Issue