如何在R中显示警告位置?

时间:2015-01-19 20:30:47

标签: r

R中有一个方便的选项show.error.locations,用于显示源文件&发生错误的行。我想知道是否有像show.warning.locations这样的东西来做同样的警告?可以派上用场,例如:

> options(show.error.locations = TRUE)
> source ("data_b-rf.R")
Error in savePlot(paste(fn, "_varimp", sep = "_"), "png") (from data_b-rf.R#149) :
  can only copy from 'X11(type="*cairo")' devices
In addition: Warning messages:
1: In grepl("\n", lines, fixed = TRUE) :
  input string 48 is invalid in this locale
2: In randomForest.default(m, y, ...) :
  The response has five or fewer unique values.  Are you sure you want to do regression?
> options("show.error.locations")
$show.error.locations
[1] TRUE

> options("show.error.messages")
$show.error.messages
[1] TRUE

这只会写出错误的位置,而不是那些警告的位置。在R中有没有办法做到这一点?

1 个答案:

答案 0 :(得分:2)

据我所知,没有办法为警告启用行号。但是你可以将options(warn=2)的警告变为错误,这当然会停止执行某项功能但通常如果你收到意外的警告,最好停下来找出原因,