[英]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 中以特定颜色打印错误和警告消息,您必须编辑编辑器主题。
.rstheme
..rstheme
的文件。my_theme.rstheme
.my_theme.rstheme
。my_theme.rstheme
with a text editormy_theme.rstheme
/* 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 */
。.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 检查器发现了这一点。)
.rstheme
file..rstheme
文件。Add...
Add...
.rstheme
file you created earlier and click Open
..rstheme
文件,然后单击Open
。 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.