From mboxrd@z Thu Jan 1 00:00:00 1970 From: Tyler Smith Subject: R output with session polluted with ascii color codes Date: Fri, 16 Mar 2018 09:57:37 -0400 Message-ID: <1521208657.1598400.1305479248.43DFD650@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:55095) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ewprZ-0006Ix-GN for emacs-orgmode@gnu.org; Fri, 16 Mar 2018 09:57:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ewprY-0000NP-3z for emacs-orgmode@gnu.org; Fri, 16 Mar 2018 09:57:41 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:45475) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ewprX-0000L4-Q3 for emacs-orgmode@gnu.org; Fri, 16 Mar 2018 09:57:40 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 0D2A720E02 for ; Fri, 16 Mar 2018 09:57:38 -0400 (EDT) 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" To: Emacs Org-Mode Help Hi, The printed output of some R functions includes ascii color codes (i.e., = =1B[3m=1B[90m). This causes problems with R code blocks evaluated with the = :session option. I've pasted a file below that demonstrates the problem. (s= hould 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 wa= y to filter out these codes from the babel side. The function of interest i= s ~org-babel-R-evaluate-session` in ob-R.el. Adding a ~(replace-regexp-in-s= tring 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 =20=20 1 5.10 3.50 1.40 0.200 setosa=20 2 4.90 3.00 1.40 0.200 setosa=20 3 4.70 3.20 1.30 0.200 setosa=20 4 4.60 3.10 1.50 0.200 setosa=20 5 5.00 3.60 1.40 0.200 setosa=20 6 5.40 3.90 1.70 0.400 setosa=20 7 4.60 3.40 1.40 0.300 setosa=20 8 5.00 3.40 1.50 0.200 setosa=20 9 4.40 2.90 1.40 0.200 setosa=20 10 4.90 3.10 1.50 0.100 setosa=20 # ... 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: =E2=80=98dplyr=E2=80=99 The following objects are masked from =E2=80=98package:stats=E2=80=99: filter, lag The following objects are masked from =E2=80=98package:base=E2=80=99: intersect, setdiff, setequal, union =1B[90m# A tibble: 150 x 5=1B[39m Sepal.Length Sepal.Width Petal.Length Petal.Width Species =1B[3m=1B[90m=1B[39m=1B[23m =1B[3m=1B[90m=1B[39m= =1B[23m =1B[3m=1B[90m=1B[39m=1B[23m =1B[3m=1B[90m=1B= [39m=1B[23m =1B[3m=1B[90m=1B[39m=1B[23m=20=20 =1B[90m 1=1B[39m 5.10 3.50 1.40 =1B[90m0=1B[39= m=1B[90m.=1B[39m200 setosa=20 =1B[90m 2=1B[39m 4.90 3.00 1.40 =1B[90m0=1B[39= m=1B[90m.=1B[39m200 setosa=20 =1B[90m 3=1B[39m 4.70 3.20 1.30 =1B[90m0=1B[39= m=1B[90m.=1B[39m200 setosa=20 =1B[90m 4=1B[39m 4.60 3.10 1.50 =1B[90m0=1B[39= m=1B[90m.=1B[39m200 setosa=20 =1B[90m 5=1B[39m 5.00 3.60 1.40 =1B[90m0=1B[39= m=1B[90m.=1B[39m200 setosa=20 =1B[90m 6=1B[39m 5.40 3.90 1.70 =1B[90m0=1B[39= m=1B[90m.=1B[39m400 setosa=20 =1B[90m 7=1B[39m 4.60 3.40 1.40 =1B[90m0=1B[39= m=1B[90m.=1B[39m300 setosa=20 =1B[90m 8=1B[39m 5.00 3.40 1.50 =1B[90m0=1B[39= m=1B[90m.=1B[39m200 setosa=20 =1B[90m 9=1B[39m 4.40 2.90 1.40 =1B[90m0=1B[39= m=1B[90m.=1B[39m200 setosa=20 =1B[90m10=1B[39m 4.90 3.10 1.50 =1B[90m0=1B[39= m=1B[90m.=1B[39m100 setosa=20 =1B[90m# ... with 140 more rows=1B[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 =20=20 1 5.10 3.50 1.40 0.200 setosa=20 2 4.90 3.00 1.40 0.200 setosa=20 3 4.70 3.20 1.30 0.200 setosa=20 4 4.60 3.10 1.50 0.200 setosa=20 5 5.00 3.60 1.40 0.200 setosa=20 6 5.40 3.90 1.70 0.400 setosa=20 7 4.60 3.40 1.40 0.300 setosa=20 8 5.00 3.40 1.50 0.200 setosa=20 9 4.40 2.90 1.40 0.200 setosa=20 10 4.90 3.10 1.50 0.100 setosa=20 # ... with 140 more rows #+end_example