Clear last error in R

I want to clear the current R session of the last error so that the next call to geterrmessage () is empty.

Example:

> stop('halt !') Error: halt ! > geterrmessage() [1] "Error: halt !\n" > something_that_cleans_the_last_error > geterrmessage() [1] "" 

thanks

+5
source share
1 answer

Since there seems to be no easy way to do this, here are a few alternatives:

Start a new instance of R and exit the old one:

 system("R"); q("no") 

This will completely clear your workspace. If you want to save your workspace, try:

 save.image(); system("R"); q("no") 

This will restore your workspace, but leave a lingering .Rdata work file. If you want to remove this:

 save.image(); system("R"); unlink('.Rdata'); q("no"); 

But you still have to reload the packages you previously downloaded. That is, if you do not do:

 lp<-(.packages()); save.image(); system("R"); unlink('.Rdata'); q("no"); rapply(as.list(lp), library); rm(lp) 

which works only from the command line, since the second line must be entered into the new R.

Note. I really do not recommend this solution.

0
source

Source: https://habr.com/ru/post/1206451/


All Articles