Skip to content

Instantly share code, notes, and snippets.

@gtuckerkellogg
Created September 23, 2021 01:52
Show Gist options
  • Save gtuckerkellogg/e356d20497cfdc8e4fc683412e320e3e to your computer and use it in GitHub Desktop.
Save gtuckerkellogg/e356d20497cfdc8e4fc683412e320e3e to your computer and use it in GitHub Desktop.
patch ob-R.el to handle output with > characters from https://orgmode.org/list/87zgxc42qg.fsf@gmail.com/
--- ob-R.el.orig 2021-09-23 09:09:47.449131259 +0800
+++ ob-R.el 2021-09-23 09:44:13.727290603 +0800
@@ -38,6 +38,8 @@
(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,26 +439,23 @@
(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))
- (with-current-buffer session
- (let ((comint-prompt-regexp (concat "^" comint-prompt-regexp)))
- (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* ((tmp-file (org-babel-temp-file "R-")))
+ (with-temp-file tmp-file
+ (insert (concat body "\n" org-babel-R-eoe-indicator)))
+ (with-current-buffer session
+ (let* ((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)))
+ (ess-send-string
+ process (format "source('%s', print.eval=TRUE)"
+ (org-babel-process-file-name tmp-file 'noquote)))
+ (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.
@gtuckerkellogg
Copy link
Author

gtuckerkellogg commented Sep 23, 2021

This is cobbled together from https://orgmode.org/list/87zgxc42qg.fsf@gmail.com/ and works for me. (applied on org-mode 9.4.6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment