get R console to highlight errors in red

2019-02-25 17:37发布

问题:

A lot of the time I paste code for evaluation into the R console. Sometimes errors occur in the middle of the statements I pasted and I miss them because they blend in with the rest of the stuff on the screen. Is there a way to make them stand out via color coding?

回答1:

On linux or mac, you can simply load the colorout package and your errors will show up in red.



回答2:

from ?base::options error: either a function or an expression governing the handling of non-catastrophic errors such as those generated by stop as well as by signals and internally detected errors. If the option is a function, a call to that function, with no arguments, is generated as the expression. The default value is NULL: see stop for the behaviour in that case. The functions dump.frames and recover provide alternatives that allow post-mortem debugging. Note that these need to specified as e.g. options(error=utils::recover) in startup files such as ‘.Rprofile’.

So, I guess you could modify your .Rprofile to handle errors differently, if stopping is too much maybe you can surround them with some line-breaks to make them stand out in your code.



回答3:

Try these:

stop("an error occurred")

message("an error occurred")

cat("error occurred", file=stderr())



回答4:

I would suggest using RStudio. It has many color options for the console and file text that are really simple to change.

Here's how mine looks. It makes error, warnings, and messages really easy to spot.