emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Jack Kamm <jackkamm@gmail.com>
To: Dylan Schwilk <dylan@schwilk.org>, emacs-orgmode@gnu.org
Subject: Re: Help debugging R source code block output problem with :session
Date: Fri, 28 Aug 2020 19:36:58 -0700	[thread overview]
Message-ID: <87k0xifa9h.fsf@gmail.com> (raw)
In-Reply-To: <87r1rqled0.fsf@havana>

[-- Attachment #1: Type: text/plain, Size: 744 bytes --]

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/


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-R-Fix-prompt-mangling-in-session-output.patch --]
[-- Type: text/x-patch, Size: 3698 bytes --]

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	other threads:[~2020-08-29  2:37 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-28 20:11 Help debugging R source code block output problem with :session Dylan Schwilk
2020-08-29  2:36 ` Jack Kamm [this message]
2020-08-29  4:07   ` Dylan Schwilk
2020-08-29  7:24     ` Jack Kamm
2020-08-29 20:35       ` Berry, Charles via General discussions about Org-mode.
2020-08-30  4:37         ` Dylan Schwilk
2020-08-30 15:08         ` Jack Kamm
2020-09-07  8:18         ` Jack Kamm
2020-09-07  8:45           ` Jack Kamm
2020-09-07 17:37             ` Berry, Charles via General discussions about Org-mode.
2020-09-07 20:07               ` Jack Kamm
2020-09-07 22:12                 ` Berry, Charles via General discussions about Org-mode.
2020-09-08  1:06                   ` Jack Kamm
2020-09-08  2:08                     ` Berry, Charles via General discussions about Org-mode.
2020-09-08 14:51                       ` Jack Kamm
2020-09-08 16:45                         ` Berry, Charles via General discussions about Org-mode.
2020-09-08 17:41                           ` Jack Kamm
2020-09-18 16:39                             ` Dylan Schwilk
2020-10-28 13:13                 ` Jack Kamm
2020-10-28 13:46                   ` Jeremie Juste
2020-08-30  4:18       ` Dylan Schwilk
2021-05-03  8:53       ` Jeremie Juste
  -- strict thread matches above, loose matches on Subject: below --
2022-01-11 23:36 John Hendy
2022-01-12  7:49 ` Jeremie Juste
2022-01-12 15:04   ` John Hendy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k0xifa9h.fsf@gmail.com \
    --to=jackkamm@gmail.com \
    --cc=dylan@schwilk.org \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).