Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the colour of the printed output in base R?

Tags:

r

I would like to change the colour of some output printed in R without importing external libraries. How can I do this?

I already know

print('this is black')

and

message('this is red')

But how can I do other colours? Green or blue, for example

Note

My question is similar to this except I am limited to using base R (no packages)

like image 338
stevec Avatar asked Jul 14 '19 19:07

stevec


People also ask

How do I color data in R?

In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.

How do I print code in R?

Print output using paste() function inside print() function R provides a method paste() to print output with string and variable together. This method defined inside the print() function. paste() converts its arguments to character strings. One can also use paste0() method.


1 Answers

You can try use this:

txt<-"test"
for(col in 29:47){ cat(paste0("\033[0;", col, "m",txt,"\033[0m","\n"))}

or if you want other function interesting i find this

https://github.com/r-lib/testthat/blob/717b02164def5c1f027d3a20b889dae35428b6d7/R/colour-text.r

like image 114
GreenLantern Avatar answered Nov 15 '22 07:11

GreenLantern