1

A similar question was asked a year ago, but the requirements were different (querent wanted R studio), and the solution package is not compatible with R 3.0.

I am using the R interpreter directly from the bash command line. I would like my scripts to output color text, ideally in a manner similar to how using a particular sequence of characters in C causes the color to be different.

More specifically, in C, we can output colors using printf as described in the answer to this question. I wonder if R 3.0.2 has a facility to do the same.

Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • The example you provided uses ANSI escape sequences to show colors. Those are interpreted by your terminal so e.g. cat("\033[32;1m OK \033[0m\n") in R will work. – Alex Vorobiev Dec 01 '13 at 23:29
  • Touche. I tried with `print` and it just gave me the characters back. If you write your commend as an answer, I will accept it. – merlin2011 Dec 02 '13 at 00:29

1 Answers1

2

The ANSI sequences in the question you mentioned are processed by the terminal emulator so they will work fine in R:

cat("\033[32;1m OK \033[0m\n")

Note that \033 is (octal) code for escape symbol. It is one (non-printable) symbol which tells the terminal to start interpreting the control sequence. print when given \033 will output the four symbols \, 0, 3, 3 literally which, of course, tells the terminal nothing. See Wikipedia for the full list of ANSI escape sequences.

Alex Vorobiev
  • 4,349
  • 21
  • 29