Dominic Comtois
Dominic Comtois

Reputation: 10401

in R / RStudio running on Windows, can we make message(), warning() and error() use different colors in the console?

I use a lot of messages e.g. message("dadida") in one of my projets, and it is becomming annoying to see all this red text everywhere, always making me wonder if there's an error or a warning hidden somewhere.

I need those messages in the end product so I can't just remove them. But if there was a way to make messages, warnings and errors appear in different colors in the console, that would solve my problem. I haven't found a way to do this.

Edit

Thanks all for your input. I hadn't realized that the red color for all those types of messages was specific to RStudio. In the RGui, all is just plain white text. If customizing the colors is not feasible in RGui, maybe it is in RStudio?

Upvotes: 5

Views: 1640

Answers (2)

JRF1111
JRF1111

Reputation: 182

To make error and warning messages print in a specific color in RStudio, you have to edit your editor theme.

  1. Find the Editor theme that you're using by going to the Appearance Preferences Pane in Global Options.

Custom Editor Themes from https://support.rstudio.com/hc/en-us/articles/115011846747-Using-RStudio-Themes#custom-editor-themes

  1. Search your machine for that editor theme (you'll probably have to replace spaces with underscores in your search). You're looking for a file with the extension .rstheme.
  2. Copy that file to a new location you can easily find again and rename it something like my_theme.rstheme.
  3. Open my_theme.rstheme with a text editor
  4. At the top of the file, you should see something like /* rs-theme-name: Example name */. Replace that with a new title for your theme: /* rs-theme-name: My theme */.
  5. At the bottom of the file, add this line: .GD15MCFCOTB{color: #FF0000;} replacing #FF0000 with whatever hex color code you want to use.

(I have no idea why, but the CSS class GD15MCFCOTB is what RStudio uses to designate warning and error messages. I discovered this by right clicking on an error message in RStudio and using the HTML inspector.)

  1. Save and close your updated .rstheme file.
  2. Go back to RStudio, navigate to the Appearance Preferences Pane in Global Options, and click the button Add...
  3. Select the .rstheme file you created earlier and click Open.
  4. You should now see your theme in the list of Editor themes. Select it and click OK. That will apply your new theme.

See this page for more info on custom themes in RStudio: https://support.rstudio.com/hc/en-us/articles/115011846747-Using-RStudio-Themes#custom-editor-themes

Upvotes: 2

Rob Hall
Rob Hall

Reputation: 1418

While I am not aware of a way of customizing the output in RGui or RStudio for Windows, using cat() instead of message() will avoid the red text in RStudio. Instead of using

message("Hello!")

use

cat("Hello!\n")

(note the \n that is needed to get a new line).

There are some subtle differences between message() and cat(), which may be relevant when you are redirecting your output e.g. to a LaTeX document using Sweave, so cat() may not be a suitable substitute in all settings.

Upvotes: 3

Related Questions