emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Tyler Smith <tyler@plantarum.ca>
To: Emacs Org-Mode Help <emacs-orgmode@gnu.org>
Subject: R output with session polluted with ascii color codes
Date: Fri, 16 Mar 2018 09:57:37 -0400	[thread overview]
Message-ID: <1521208657.1598400.1305479248.43DFD650@webmail.messagingengine.com> (raw)

Hi,

The printed output of some R functions includes ascii color codes (i.e., ^[[3m^[[90m). This causes problems with R code blocks evaluated with the :session option. I've pasted a file below that demonstrates the problem. (should I attach it as a file? not sure what's preferable).

I can't find a way to disable this feature from R. However, I did find a way to filter out these codes from the babel side. The function of interest is ~org-babel-R-evaluate-session` in ob-R.el. Adding a ~(replace-regexp-in-string ansi-color-control-seq-regexp "" ...)~ form around the output strips away the color codes so they don't mess up the output.  I've included that code in the example. If that makes sense I can put that together as a patch. If that doesn't make sense, please let me know how to fix this!

Best,

Tyler

* Reproducible Example

Start with ~emacs -Q~, then evaluate each of the following code blocks
in turn.

#+BEGIN_SRC elisp setup
(require 'package)
(setq package-load-list
      '((org-plus-contrib t)
	(ess t)
	(julia-mode t)))
(package-initialize)
(require 'org)
(require 'ess)

(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . t)
   (R . t)
   (shell . t)))
#+END_SRC

#+RESULTS:

If you don't already have ~tidyr~ and ~dplyr~ installed, you need to
do that before running the following:

#+BEGIN_SRC R :results output
library(tidyr)
library(dplyr)
as_tibble(iris)
#+END_SRC

#+RESULTS:
#+begin_example
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1         5.10        3.50         1.40       0.200 setosa 
 2         4.90        3.00         1.40       0.200 setosa 
 3         4.70        3.20         1.30       0.200 setosa 
 4         4.60        3.10         1.50       0.200 setosa 
 5         5.00        3.60         1.40       0.200 setosa 
 6         5.40        3.90         1.70       0.400 setosa 
 7         4.60        3.40         1.40       0.300 setosa 
 8         5.00        3.40         1.50       0.200 setosa 
 9         4.40        2.90         1.40       0.200 setosa 
10         4.90        3.10         1.50       0.100 setosa 
# ... with 140 more rows
#+end_example

#+BEGIN_SRC R :results output :session RSESSION
library(tidyr)
library(dplyr)
as_tibble(iris)
#+END_SRC

#+RESULTS:
#+begin_example

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union
^[[90m# A tibble: 150 x 5^[[39m
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          ^[[3m^[[90m<dbl>^[[39m^[[23m       ^[[3m^[[90m<dbl>^[[39m^[[23m        ^[[3m^[[90m<dbl>^[[39m^[[23m       ^[[3m^[[90m<dbl>^[[39m^[[23m ^[[3m^[[90m<fct>^[[39m^[[23m  
^[[90m 1^[[39m         5.10        3.50         1.40       ^[[90m0^[[39m^[[90m.^[[39m200 setosa 
^[[90m 2^[[39m         4.90        3.00         1.40       ^[[90m0^[[39m^[[90m.^[[39m200 setosa 
^[[90m 3^[[39m         4.70        3.20         1.30       ^[[90m0^[[39m^[[90m.^[[39m200 setosa 
^[[90m 4^[[39m         4.60        3.10         1.50       ^[[90m0^[[39m^[[90m.^[[39m200 setosa 
^[[90m 5^[[39m         5.00        3.60         1.40       ^[[90m0^[[39m^[[90m.^[[39m200 setosa 
^[[90m 6^[[39m         5.40        3.90         1.70       ^[[90m0^[[39m^[[90m.^[[39m400 setosa 
^[[90m 7^[[39m         4.60        3.40         1.40       ^[[90m0^[[39m^[[90m.^[[39m300 setosa 
^[[90m 8^[[39m         5.00        3.40         1.50       ^[[90m0^[[39m^[[90m.^[[39m200 setosa 
^[[90m 9^[[39m         4.40        2.90         1.40       ^[[90m0^[[39m^[[90m.^[[39m200 setosa 
^[[90m10^[[39m         4.90        3.10         1.50       ^[[90m0^[[39m^[[90m.^[[39m100 setosa 
^[[90m# ... with 140 more rows^[[39m
#+end_example

* Proposed Fix

Evaluating the following replaces the built-in function with my fix:

#+BEGIN_SRC elisp fix
(defun org-babel-R-evaluate-session
    (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
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
     ;; strip ansi-color-control-seq-regexp from output!!
     (replace-regexp-in-string
      ansi-color-control-seq-regexp ""
      (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")))))

#+END_SRC

#+RESULTS:
: org-babel-R-evaluate-session

#+BEGIN_SRC R :results output :session RSESSION
as_tibble(iris)
#+END_SRC

#+RESULTS:
#+begin_example
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1         5.10        3.50         1.40       0.200 setosa 
 2         4.90        3.00         1.40       0.200 setosa 
 3         4.70        3.20         1.30       0.200 setosa 
 4         4.60        3.10         1.50       0.200 setosa 
 5         5.00        3.60         1.40       0.200 setosa 
 6         5.40        3.90         1.70       0.400 setosa 
 7         4.60        3.40         1.40       0.300 setosa 
 8         5.00        3.40         1.50       0.200 setosa 
 9         4.40        2.90         1.40       0.200 setosa 
10         4.90        3.10         1.50       0.100 setosa 
# ... with 140 more rows
#+end_example

             reply	other threads:[~2018-03-16 13:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-16 13:57 Tyler Smith [this message]
2018-03-16 15:25 ` R output with session polluted with ascii color codes William Denton
2018-03-16 17:23   ` Tyler Smith
2018-03-16 18:55     ` William Denton
2018-03-16 19:22       ` Tyler Smith

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=1521208657.1598400.1305479248.43DFD650@webmail.messagingengine.com \
    --to=tyler@plantarum.ca \
    --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).