14

I am wondering if it is possible to specify color options when displaying text with the cat command or a similar command. The reason I ask is because I have set up and interactive feedback system with a simulation I am working on and would like to provide more information to the display as the simulation which takes hours to run is being processed.

Francis Smart
  • 3,875
  • 6
  • 32
  • 58
  • 1
    1) http://stackoverflow.com/questions/10802806/is-there-a-way-to-output-some-text-to-the-r-console-in-colors, 2) http://stackoverflow.com/questions/10621049/color-syntax-highlighting-in-bash-while-using-r, 3) http://stackoverflow.com/questions/14465962/get-r-console-to-highlight-errors-in-red – GSee Jul 26 '14 at 14:23

2 Answers2

16

See this file from the testthat package for an excellent example on how to create colored text: https://github.com/hadley/testthat/blob/717b02164def5c1f027d3a20b889dae35428b6d7/R/colour-text.r

You can just use this colourise function to add color to your text. The file also has a bunch of predefined colors.

EDIT

There is now an R package for ANSI colors on CRAN, it is called crayon. Disclaimer: I am the package author. http://cran.r-project.org/web/packages/crayon/index.html https://github.com/gaborcsardi/crayon

Gabor Csardi
  • 10,705
  • 1
  • 36
  • 53
  • That's great for use on xterm. It doesn't work on Rstudio, though. You could always try `message("will be red")`. – Steve Pitchers Aug 29 '14 at 17:13
  • Rstudio does not provide you a real terminal, so unless they have their own API, you cannot do anything there, except for `message` as you say, but `message` is not always good. – Gabor Csardi Aug 29 '14 at 21:39
  • Rstudio does use colors for syntax highlight, so it must be technically possible - but how? – Steve Pitchers Aug 31 '14 at 13:20
  • I guess you can take the source code and implement it if you want. They might also implement it at some point: https://support.rstudio.com/hc/communities/public/questions/200652873-Console-different-colors?locale=en-us – Gabor Csardi Aug 31 '14 at 19:18
-4

In RStudio:

Tools --> Global Options --> Code --> Display --> Show syntax highlighting in console input