Automatically coloring your R output in the terminal using colorout

[This article was first published on Fellgernon Bit - rstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Thanks to Alyssa Frazee I just learned about the colorout package (Aquino, 2013). It modifies R so that the output is in different colors, making it much more pleasant to use R in the terminal.

Do note that colorout is not available from CRAN, but you can easily install by following the instructions on the colorout site (Official site) reproduced below:

download.file<span class="p">(</span><span class="s">"http://www.lepem.ufc.br/jaa/colorout_1.0-2.tar.gz"</span><span class="p">,</span> destfile <span class="o">=</span> <span class="s">"colorout_1.0-2.tar.gz"</span><span class="p">)</span>
install.packages<span class="p">(</span><span class="s">"colorout_1.0-2.tar.gz"</span><span class="p">,</span> type <span class="o">=</span> <span class="s">"source"</span><span class="p">,</span> repos <span class="o">=</span> <span class="kc">NULL</span><span class="p">)</span>

The next step is to then load colorout automatically when I start R. The problem is that I don’t use R solely on the terminal. I easily figured out how to do so thanks to the error message you get when attempting to load colorout on the R GUI. I thus ended up adding the following lines to my .Rprofile (both locally and in the cluster):

<span class="c1">## Change colors when running R in the terminal</span>
<span class="kr">if</span> <span class="p">(</span>Sys.getenv<span class="p">(</span><span class="s">"TERM"</span><span class="p">)</span> <span class="o">==</span> <span class="s">"xterm-256color"</span><span class="p">)</span> library<span class="p">(</span><span class="s">"colorout"</span><span class="p">)</span>

Now I have pretty R output in the terminal! Thanks again Alyssa! See her original tweet below:

colorout has been around for a while and was even at CRAN for some time. I guess that I’m just late to the party.

If the default colorout colors don’t work for you, check functions such as setOutputColors256. This post shows how you can do it and includes screenshots of the output. Other package details are included here and here.

References

Citations made with knitcitations (Boettiger, 2014).

Reproducibility

sessionInfo<span class="p">()</span>
## R version 3.0.2 (2013-09-25)
## Platform: x86_64-apple-darwin10.8.0 (64-bit)
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] knitcitations_0.5-0 bibtex_0.3-6        knitr_1.5          
## 
## loaded via a namespace (and not attached):
##  [1] codetools_0.2-8 digest_0.6.4    evaluate_0.5.1  formatR_0.10   
##  [5] httr_0.2        RCurl_1.95-4.1  stringr_0.6.2   tools_3.0.2    
##  [9] XML_3.95-0.2    xtable_1.7-1

Check other topics on #rstats.

To leave a comment for the author, please follow the link and comment on their blog: Fellgernon Bit - rstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)