emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Jack Kamm <jackkamm@gmail.com>
To: "Berry, Charles" <ccberry@health.ucsd.edu>
Cc: "emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>,
	Dylan Schwilk <dylan@schwilk.org>
Subject: Re: Help debugging R source code block output problem with :session
Date: Mon, 07 Sep 2020 13:07:31 -0700	[thread overview]
Message-ID: <87ft7t9wqk.fsf@gmail.com> (raw)
In-Reply-To: <48AD2294-E831-44D7-B564-0F7B4019C61B@health.ucsd.edu>

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

Hi Chuck,

> This does not work for my remote session.

Thanks for testing this.

The remote tests I originally tried did not cover this case -- I only
tested the case where both org-file and session were remote, but didn't
test the case where org-file was local and session was remote.

I've now tested this case as well, and added some fixes for it. I'm
reattaching the patch in full.

However, this implementation does require the R session to have the
correct default-directory -- see details below.

> The problem is that tempfiles on the remote host are like "/tmp/RtmpeFHudh/file23a66d2fc1f9", but emacs tries to use '/var/folders/kb/2hchpbyj7lb6z76l0q73w_fh0000gn/T/babel-OSXKNd/R-oNOVVB'
>
> `org-babel-temp-file' doesn't honor remote connections AFAICS.

org-babel-temp-file does honor remote connections, but relies on
default-directory to do so. I've moved the call to org-babel-temp-file
so it happens when the session buffer is current -- that way, the
tempfile will be correctly handled, assuming that the session's
default-directory is on the remote location.

If the session's default-directory isn't at the remote location, then
this will break. But, in this case some other things break as well, for
example inserting links to remote plots.

> Maybe there is some comint or tramp idiom that would solve this, but I do not know what it is.

Here are some ways to start a remote R session with correct
default-directory:

1. Do "M-x R", then when it prompts for a directory, use
"/scp:hostname:/some/path". Optionally, first visit that location with
"C-x C-f /scp:hostname:/some/path", so that the default value is
already there.

2. Alternatively, start the R session by evaluating a source block with
header argument ":dir /scp:hostname:/some/path".

3. If you prefer to use "M-x shell" with "ess-remote",
first visit the remote location with "C-x C-f /scp:hostname:/some/path", before calling "M-x shell".

4. If you prefer to start "M-x shell" locally and then
ssh in, it's still possible to have default-directory set, but it
requires some configuration [1]. Or you could use "M-x cd" to set it as well.

[1] https://emacs.stackexchange.com/questions/5589/automatically-update-default-directory-when-pwd-changes-in-shell-mode-and-term-m/5592#5592

Best,
Jack


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-R-Fix-session-output-with-substrings-matching-pro.patch --]
[-- Type: text/x-patch, Size: 4289 bytes --]

From e7f1a59167de88fb9a5b96a0e1ac3199f105f600 Mon Sep 17 00:00:00 2001
From: Jack Kamm <jackkamm@gmail.com>
Date: Mon, 7 Sep 2020 00:41:52 -0700
Subject: [PATCH] ob-R: Fix session output with substrings matching prompts

* lisp/ob-R.el (ess-send-string): Declare external function.
(org-babel-R-evaluate-session): New implementation for session output
results, that replaces calls to org-babel-comint-with-output with
custom code.
* testing/lisp/test-ob-R.el (test-ob-R/prompt-output): New test for
output results containing angle brackets.
(test-ob-R/output-nonprinted): New test for output results that aren't
explicitly printed.

Fixes issue reported in
https://orgmode.org/list/875zgjh8wn.fsf@gmail.com/,
https://orgmode.org/list/87r1rqled0.fsf@havana/
---
 lisp/ob-R.el              | 38 ++++++++++++++++++++------------------
 testing/lisp/test-ob-R.el | 13 +++++++++++++
 2 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 5e9d35f58..dffbbe112 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -38,6 +38,8 @@ (declare-function ess-make-buffer-current "ext:ess-inf" ())
 (declare-function ess-eval-buffer "ext:ess-inf" (vis))
 (declare-function ess-wait-for-process "ext:ess-inf"
 		  (&optional proc sec-prompt wait force-redisplay))
+(declare-function ess-send-string "ext:ess-inf"
+		  (process string &optional visibly message _type))
 
 (defconst org-babel-header-args:R
   '((width		 . :any)
@@ -437,24 +439,24 @@ (defun org-babel-R-evaluate-session
 	  (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"))))
+     (with-current-buffer session
+       (let* ((tmp-file (org-babel-temp-file "R-"))
+	      (process (get-buffer-process (current-buffer)))
+	      (string-buffer "")
+	      (comint-output-filter-functions
+	       (cons (lambda (text) (setq string-buffer
+					  (concat string-buffer text)))
+		     comint-output-filter-functions)))
+	 (with-temp-file tmp-file
+	   (insert body))
+	 (ess-send-string
+	  process (format "tryCatch(source('%s', print.eval=TRUE), finally=print(%s))"
+			  (org-babel-process-file-name tmp-file 'noquote)
+			  org-babel-R-eoe-indicator))
+	 (while (not (string-match (regexp-quote org-babel-R-eoe-output)
+				   string-buffer))
+	   (accept-process-output process))
+	 (substring string-buffer 0 (match-beginning 0)))))))
 
 (defun org-babel-R-process-value-result (result column-names-p)
   "R-specific processing of return value.
diff --git a/testing/lisp/test-ob-R.el b/testing/lisp/test-ob-R.el
index 7ce340ba4..ff7ea19d5 100644
--- a/testing/lisp/test-ob-R.el
+++ b/testing/lisp/test-ob-R.el
@@ -97,6 +97,19 @@ (ert-deftest test-ob-R/results-file ()
      (org-babel-goto-named-result "TESTSRC") (forward-line 1)
      (should (string= "[[file:junk/test.org]]"
 		      (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
+
+(ert-deftest test-ob-R/prompt-output ()
+  (let (ess-ask-for-ess-directory ess-history-file)
+    (org-test-with-temp-text
+     "#+begin_src R :results output :session\nprint(\"<X> <Y> <!>\")\nprint(\"one <two> three\")\nprint(\"end\")\n#+end_src\n"
+     (should (string= "[1] \"<X> <Y> <!>\"\n[1] \"one <two> three\"\n[1] \"end\"\n" (org-babel-execute-src-block))))))
+
+(ert-deftest test-ob-R/output-nonprinted ()
+  (let (ess-ask-for-ess-directory ess-history-file)
+    (org-test-with-temp-text
+     "#+begin_src R :results output :session\n4.0 * 3.5\nlog(10)\nlog10(10)\n(3 + 1) * 5\n3^-1\n1/0\n#+end_src\n"
+     (should (string= "[1] 14\n[1] 2.302585\n[1] 1\n[1] 20\n[1] 0.3333333\n[1] Inf\n" (org-babel-execute-src-block))))))
+
 (provide 'test-ob-R)
 
 ;;; test-ob-R.el ends here
-- 
2.28.0


  reply	other threads:[~2020-09-07 20:08 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
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 [this message]
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=87ft7t9wqk.fsf@gmail.com \
    --to=jackkamm@gmail.com \
    --cc=ccberry@health.ucsd.edu \
    --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).