4

I want to output a partially colored string. e.g. something like:

print(paste("This is how", style("Red","red", "bold"), "looks."))

[1] "This is how Red looks."

The string Red should be colored in red.

Any help will be much appreciated!!

Shambho
  • 3,250
  • 1
  • 24
  • 37
  • You generally just get one color printed to the console, looks like there is an option for this though http://stackoverflow.com/questions/10802806/is-there-a-way-to-output-some-text-to-the-r-console-in-colors?rq=1 using `xterm256` package – sckott Apr 17 '14 at 23:29
  • Depends on what you mean by "output." Agstudy shows how to plot it; if you want a text document, you're best off learning to generate LaTex or HTML code in R. – Carl Witthoft Apr 18 '14 at 01:39
  • Thanks a lot everyone for your time. @ScottChamberlain : I am not sure why is it so difficult. – Shambho Apr 18 '14 at 03:30
  • @CarlWitthoft : I meant output on screen. Even an html file output would be great. – Shambho Apr 18 '14 at 03:31
  • If you are using text in a R markdown document you could use `This is how Red looks`. I tried `xterm256` - it does work in the R terminal, but didn't work in RStudio. – sckott Apr 18 '14 at 13:21
  • Similar question: https://stackoverflow.com/questions/10802806/is-there-a-way-to-output-text-to-the-r-console-in-color – Waldir Leoncio May 11 '19 at 08:57

2 Answers2

1

You can hack it using text:

plot(1:10,1:10,type='n',frame.plot=F,axes=F,xlab="",ylab="")
text(5,1,"This is how")
text(5.8,1,"Red",col='red',font=2)
text(6.3,1,"looks.")

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261
0

Perhaps the crayon package can help you:

library(crayon)
cat("This is how", red("Red"), "looks.\n")

Output:

enter image description here

If you want more features, perhaps the xterm256 package will be helpful.

Community
  • 1
  • 1
Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107