From mboxrd@z Thu Jan 1 00:00:00 1970 From: Aaron Ecay Subject: Re: R code block produces only partial output Date: Sat, 23 Aug 2014 13:10:36 -0400 Message-ID: <87tx53p1wz.fsf@gmail.com> References: <87iom8zd24.fsf@gmail.com> <877g2oz9gv.fsf@gmail.com> <87lhr27oap.fsf@gmail.com> <87r40uwavs.fsf@gmail.com> <8761i5kg8f.fsf@gmail.com> <87ppgcrg8n.fsf@gmail.com> <87lhr0qimr.fsf@gmail.com> <87wqa9owhv.fsf@gmail.com> <87oavkp2xa.fsf@gmail.com> <87r40f8xfo.fsf@Rainer.invalid> <87a9718hg7.fsf@gmail.com> <877g2511mx.fsf@Rainer.invalid> <871ts7r4ho.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:55904) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XLEq0-0002Zy-CP for emacs-orgmode@gnu.org; Sat, 23 Aug 2014 13:10:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XLEpr-0005bH-9h for emacs-orgmode@gnu.org; Sat, 23 Aug 2014 13:10:48 -0400 Received: from mail-qg0-x235.google.com ([2607:f8b0:400d:c04::235]:60497) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XLEpr-0005bC-4J for emacs-orgmode@gnu.org; Sat, 23 Aug 2014 13:10:39 -0400 Received: by mail-qg0-f53.google.com with SMTP id z60so8096662qgd.40 for ; Sat, 23 Aug 2014 10:10:38 -0700 (PDT) Received: from localhost (c-68-81-204-26.hsd1.pa.comcast.net. [68.81.204.26]) by mx.google.com with ESMTPSA id c13sm65563324qaa.11.2014.08.23.10.10.37 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 23 Aug 2014 10:10:37 -0700 (PDT) In-Reply-To: <871ts7r4ho.fsf@gmail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Here=E2=80=99s another version of the previous patch which fixes a couple b= ugs related to 1) changing the window configuration unwarrantedly 2) syntactically invalid R code (mismatched parens or quotes) could hang emacs It=E2=80=99s easy to get R to throw an error in the mismatched-parens case,= but more difficult to recover from the mismatched-quotes one. Thus, when babel detects such a case it bails and signals a user-error in emacs. (Under the present code in master, both the mismatched-parens and mismatched-quotes cases hang emacs pending a manual C-g.) The patch is now firmly into the territory of gross hacks, so I=E2=80=99d m= uch prefer the evaluate-based approach if it can be made palatable. --=-=-= Content-Type: text/x-diff; charset=utf-8 Content-Disposition: inline; filename=0001-ob-R-overhaul-handling-of-output-results-type-in-a-s.patch Content-Transfer-Encoding: quoted-printable >From afa24835f2a170c59a044dcf3bcf0ee765e8b568 Mon Sep 17 00:00:00 2001 From: Aaron Ecay Date: Sat, 16 Aug 2014 00:49:05 -0400 Subject: [PATCH] ob-R: overhaul handling of :output results type in a sessi= on MIME-Version: 1.0 Content-Type: text/plain; charset=3DUTF-8 Content-Transfer-Encoding: 8bit * (org-babel-R-evaluate-session): Capture output using =E2=80=98ess-execute= =E2=80=99. This function dumps the output from R into a buffer; we then use some tricks to clean it of spurious text. --- lisp/ob-R.el | 69 ++++++++++++++++++++++++++++++++++++++++++++------------= ---- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index 41b943c..fbd4af4 100644 --- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -364,9 +364,10 @@ last statement in BODY, as elisp." (output (org-babel-eval org-babel-R-command body)))) =20 (defvar ess-eval-visibly-p) +(defvar ess-execute-in-process-buffer) =20 (defun org-babel-R-evaluate-session - (session body result-type result-params column-names-p row-names-p) + (session body result-type result-params column-names-p row-names-p) "Evaluate BODY in SESSION. If RESULT-TYPE equals 'output then return standard output as a string. If RESULT-TYPE equals 'value then return the value of the @@ -396,23 +397,55 @@ last statement in BODY, as elisp." (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 - "^\\([ ]*[>+\\.][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line) - (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* ((sentinel-file (org-babel-temp-file "R-"))) + ;; This code borrowed from + ;; `org-babel-comint-eval-invisibly-and-wait-for-file' + (unless (org-babel-comint-buffer-livep session) + (error "Buffer %s does not exist or has no process" session)) + (if (file-exists-p sentinel-file) (delete-file sentinel-file)) + (with-current-buffer session + (let ((ess-execute-in-process-buffer nil) + (temp-buffer-show-function #'ignore)) + (ess-execute (format + " +.org.old.opts <- options(prompt =3D \"# Org babel prompt\\n\", continue = =3D \"# Org babel prompt\\n\") +cat(\"# Org babel begin output\n\") +{ +%s +#ORG-BABEL-OOPS\"#' +} +cat(\"# Org babel end output\n\") +options(.org.old.opts) +rm(.org.old.opts) +file.create(%S) +" + (org-babel-chomp body) + (if (tramp-tramp-file-p sentinel-file) + (tramp-file-name-localname + (tramp-dissect-file-name sentinel-file)) + sentinel-file)) + nil "org-babel-R-output")) + ;; From Tramp 2.1.19 the following cache flush is not necessary + (if (file-remote-p default-directory) + (let (v) + (with-parsed-tramp-file-name default-directory nil + (tramp-flush-directory-property v ""))))) + (while (not (file-exists-p sentinel-file)) (sit-for 0.25)) + ;; End borrowed code. + (with-current-buffer "*org-babel-R-output*" + (goto-char (point-min)) + (when (search-forward "ORG-BABEL-OOPS") + (user-error "Mismatched quotation marks in babel code; could not parse= output.")) + (search-forward "# Org babel begin output") + (delete-region (point-min) (1+ (point))) + (goto-char (point-max)) + (search-backward "# Org babel end output") + (delete-region (point) (point-max)) + (goto-char (point-min)) + (flush-lines "^$") + (flush-lines "^# Org babel prompt$") + (delete-trailing-whitespace) + (buffer-string)))))) =20 (defun org-babel-R-process-value-result (result column-names-p) "R-specific processing of return value. --=20 2.0.4 --=-=-= Content-Type: text/plain -- Aaron Ecay --=-=-=--