R - How to output a partially colored string in R?

混江龙づ霸主 提交于 2021-02-10 09:47:06

问题


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!!


回答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




回答2:


Perhaps the crayon package can help you:

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

Output:

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



来源:https://stackoverflow.com/questions/23145079/r-how-to-output-a-partially-colored-string-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!