emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help debugging R source code block output problem with :session


From: Jack Kamm
Subject: Re: Help debugging R source code block output problem with :session
Date: Fri, 28 Aug 2020 19:36:58 -0700

Hi Dylan,

I'm able to reproduce your error. I think this same bug has been
previously reported at [1]. As discussed there, the reason is that
substrings ending in ">" get stripped out by
org-babel-comint-with-output because it thinks they are prompts (in
particular, they match comint-prompt-regexp).

I'm attaching a patch to solve the issue. It replaces
org-babel-comint-with-output, with an R call to capture.output. I think
this approach is simpler and more robust, since it doesn't need to do
any parsing to remove prompts or other unwanted outputs.

I haven't committed to ob-R.el before, so thought it would be best to
solicit feedback here before merging this in.

Cheers,
Jack

[1] https://orgmode.org/list/875zgjh8wn.fsf@gmail.com/

>From 1dc8e2d2cb01a4e6b82342ea8d8c965df8f5222c Mon Sep 17 00:00:00 2001
From: Jack Kamm <jackkamm@gmail.com>
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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]