简体   繁体   English

在 Windows 上运行的 R/RStudio 中,我们可以让 message()、warning() 和 error() 在控制台中使用不同的颜色吗?

[英]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 eg 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.我在我的一个项目中使用了很多消息,例如message("dadida") ,到处看到所有这些红色文本变得很烦人,总是让我想知道是否有错误或警告隐藏在某处。

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.我没有意识到所有这些类型的消息的红色都是 RStudio 特有的。 In the RGui, all is just plain white text.在 RGui 中,一切都只是纯白色的文本。 If customizing the colors is not feasible in RGui, maybe it is in RStudio?如果在 RGui 中自定义颜色不可行,那么在 RStudio 中是否可行?

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.虽然我不知道在 Windows 的 RGui 或 RStudio 中自定义输出的方法,但使用 cat() 而不是 message() 将避免 RStudio 中的红色文本。 Instead of using而不是使用

message("Hello!")

use

cat("Hello!\n")

(note the \\n that is needed to get a new line). (请注意获取新行所需的 \\n)。

There are some subtle differences between message() and cat(), which may be relevant when you are redirecting your output eg to a LaTeX document using Sweave, so cat() may not be a suitable substitute in all settings. message() 和 cat() 之间有一些细微的区别,当您使用 Sweave 将输出重定向到例如 LaTeX 文档时,这可能是相关的,因此 cat() 可能不是所有设置中的合适替代品。

To make error and warning messages print in a specific color in RStudio, you have to edit your editor theme.要在 RStudio 中以特定颜色打印错误和警告消息,您必须编辑编辑器主题。

  1. Find the Editor theme that you're using by going to the Appearance Preferences Pane in Global Options.转到全局选项中的外观首选项窗格,找到您正在使用的编辑器主题

来自 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 .您正在寻找扩展名为.rstheme的文件。
  2. Copy that file to a new location you can easily find again and rename it something like my_theme.rstheme .将该文件复制到您可以轻松找到的新位置,并将其重命名为my_theme.rstheme
  3. Open my_theme.rstheme with a text editor使用文本编辑器打开my_theme.rstheme
  4. At the top of the file, you should see something like /* rs-theme-name: Example name */ .在文件的顶部,您应该会看到类似/* rs-theme-name: Example name */ Replace that with a new title for your theme: /* rs-theme-name: My 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.在文件底部,添加以下行: .GD15MCFCOTB{color: #FF0000;}用您想要使用的任何十六进制颜色代码替换#FF0000

(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.) (我不知道为什么,但是 RStudio 使用 CSS 类GD15MCFCOTB来指定警告和错误消息。我通过右键单击 RStudio 中的错误消息并使用 HTML 检查器发现了这一点。)

  1. Save and close your updated .rstheme file.保存并关闭更新的.rstheme文件。
  2. Go back to RStudio, navigate to the Appearance Preferences Pane in Global Options, and click the button Add...返回 RStudio,导航到全局选项中的外观首选项窗格,然后单击按钮Add...
  3. Select the .rstheme file you created earlier and click Open .选择您之前创建的.rstheme文件,然后单击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有关 RStudio 中自定义主题的更多信息,请参阅此页面: https : //support.rstudio.com/hc/en-us/articles/115011846747-Using-RStudio-Themes#custom-editor-themes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM