From 08cdd0557967d69a093b1642a4467e800fe84c4b Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Mon, 30 Aug 2010 09:26:02 -0700 Subject: [PATCH] babel: Cleaner session evaluation for R in :results value case * ob-comint.el (org-babel-comint-eval-invisibly-and-wait-for-file): New function to evaluate code invisibly and block until output file exists. * ob-R.el (org-babel-R-evaluate-session): Use `ess-eval-buffer' to evaluate R code in session for :results value. Write result to file invisibly using new function `org-babel-comint-eval-invisibly-and-wait-for-file'. These changes move to using standard ESS code evaluation in R sessions in the :results value case, which avoids unnecessary output to the comint buffer. In addition, the R command responsible for writing the result to file is hidden from the user. --- lisp/ob-R.el | 17 ++++++++--------- lisp/ob-comint.el | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index d16b659f3..e726d034a 100644 --- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -259,21 +259,20 @@ string. If RESULT-TYPE equals 'value then return the value of the last statement in BODY, as elisp." (case result-type (value - (let ((tmp-file (org-babel-temp-file "R-")) - broke) - (org-babel-comint-with-output (session org-babel-R-eoe-output) - (insert (mapconcat - #'org-babel-chomp - (list - body + (with-temp-buffer + (insert (org-babel-chomp body)) + (let ((ess-local-process-name + (process-name (get-buffer-process session)))) + (ess-eval-buffer nil))) + (let ((tmp-file (org-babel-temp-file "R-"))) + (org-babel-comint-eval-invisibly-and-wait-for-file + session (org-babel-maybe-remote-file tmp-file) (format org-babel-R-wrapper-lastvar tmp-file (if row-names-p "TRUE" "FALSE") (if column-names-p (if row-names-p "NA" "TRUE") "FALSE")) - org-babel-R-eoe-indicator) "\n")) - (inferior-ess-send-input)) (org-babel-R-process-value-result (org-babel-import-elisp-from-file (org-babel-maybe-remote-file tmp-file) '(16)) column-names-p))) diff --git a/lisp/ob-comint.el b/lisp/ob-comint.el index c7d3d14a0..ff7f0cba9 100644 --- a/lisp/ob-comint.el +++ b/lisp/ob-comint.el @@ -136,6 +136,23 @@ statement (not large blocks of code)." "comint-highlight-prompt")))) (accept-process-output (get-buffer-process buffer))))) +(defun org-babel-comint-eval-invisibly-and-wait-for-file + (buffer file string &optional period) + "Evaluate STRING in BUFFER invisibly. +Don't return until FILE exists. Code in STRING must ensure that +FILE exists at end of evaluation." + (unless (org-babel-comint-buffer-livep buffer) + (error "buffer %s doesn't exist or has no process" buffer)) + (if (file-exists-p file) (delete-file file)) + (process-send-string + (get-buffer-process buffer) + (if (string-match "\n$" string) string (concat string "\n"))) + ;; From Tramp 2.1.19 the following cache flush is not necessary + (if (file-remote-p default-directory) + (with-parsed-tramp-file-name default-directory nil + (tramp-flush-directory-property v ""))) + (while (not (file-exists-p file)) (sit-for (or period 0.25)))) + (provide 'ob-comint) ;; arch-tag: 9adddce6-0864-4be3-b0b5-6c5157dc7889