From 1dc8e2d2cb01a4e6b82342ea8d8c965df8f5222c Mon Sep 17 00:00:00 2001 From: Jack Kamm Date: Fri, 28 Aug 2020 19:16:05 -0700 Subject: [PATCH] ob-R: Fix prompt mangling in session output * lisp/ob-R.el (org-babel-R-evaluate-session): Replace call to org-babel-comint-with-output with ess-send-to-buffer and additional wrapper R code. Fixes https://orgmode.org/list/875zgjh8wn.fsf@gmail.com/ and https://orgmode.org/list/87r1rqled0.fsf@havana/ --- lisp/ob-R.el | 79 ++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 43 deletions(-) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index 5e9d35f58..d69cf23db 100644 --- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -412,49 +412,42 @@ (defun org-babel-R-evaluate-session If RESULT-TYPE equals `output' then return standard output as a string. If RESULT-TYPE equals `value' then return the value of the last statement in BODY, as elisp." - (cl-case result-type - (value - (with-temp-buffer - (insert (org-babel-chomp body)) - (let ((ess-local-process-name - (process-name (get-buffer-process session))) - (ess-eval-visibly-p nil)) - (ess-eval-buffer nil))) - (let ((tmp-file (org-babel-temp-file "R-"))) - (org-babel-comint-eval-invisibly-and-wait-for-file - session tmp-file - (format org-babel-R-write-object-command - (if row-names-p "TRUE" "FALSE") - (if column-names-p - (if row-names-p "NA" "TRUE") - "FALSE") - ".Last.value" (org-babel-process-file-name tmp-file 'noquote))) - (org-babel-R-process-value-result - (org-babel-result-cond result-params - (with-temp-buffer - (insert-file-contents tmp-file) - (org-babel-chomp (buffer-string) "\n")) - (org-babel-import-elisp-from-file tmp-file '(16))) - column-names-p))) - (output - (mapconcat - 'org-babel-chomp - (butlast - (delq nil - (mapcar - (lambda (line) (when (> (length line) 0) line)) - (mapcar - (lambda (line) ;; cleanup extra prompts left in output - (if (string-match - "^\\([>+.]\\([ ][>.+]\\)*[ ]\\)" - (car (split-string line "\n"))) - (substring line (match-end 1)) - line)) - (org-babel-comint-with-output (session org-babel-R-eoe-output) - (insert (mapconcat 'org-babel-chomp - (list body org-babel-R-eoe-indicator) - "\n")) - (inferior-ess-send-input)))))) "\n")))) + (let ((ess-local-process-name + (process-name (get-buffer-process session))) + (ess-eval-visibly-p nil)) + (cl-case result-type + (value + (with-temp-buffer + (insert (org-babel-chomp body)) + (ess-eval-buffer nil)) + (let ((tmp-file (org-babel-temp-file "R-"))) + (org-babel-comint-eval-invisibly-and-wait-for-file + session tmp-file + (format org-babel-R-write-object-command + (if row-names-p "TRUE" "FALSE") + (if column-names-p + (if row-names-p "NA" "TRUE") + "FALSE") + ".Last.value" (org-babel-process-file-name tmp-file 'noquote))) + (org-babel-R-process-value-result + (org-babel-result-cond result-params + (with-temp-buffer + (insert-file-contents tmp-file) + (org-babel-chomp (buffer-string) "\n")) + (org-babel-import-elisp-from-file tmp-file '(16))) + column-names-p))) + (output + (let ((tmp-file (org-babel-temp-file "R-"))) + (with-temp-buffer + (insert (format "capture.output({ +%s +}, file='%s')" + body tmp-file)) + (ess-eval-buffer)) + (ess-wait-for-process) + (with-temp-buffer + (insert-file-contents tmp-file) + (buffer-string))))))) (defun org-babel-R-process-value-result (result column-names-p) "R-specific processing of return value. -- 2.28.0