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
My question is similar to this except I am limited to using base R (no packages)
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With