R:是否有一个命令结束一个文件,说明是否发生错误? [英] R: is there a command for the end of a file that states whether any errors occurred?
问题描述
我总是向上滚动所有代码,以目视检查错误消息或警告,并一直认为如果R简单地告诉我代码底部是否发生任何错误消息或警告。
可以这样做吗?我想即使R可以这样做,我需要一段时间来发展命令行的信任,以捕获所有错误消息或警告消息。
使用SAS我曾经使用找到命令并在日志窗口中搜索错误或警告。
感谢您对此的任何想法或建议。
这是一个非常简单的R代码示例,返回3个错误消息。
x< - c(1,2,3,4)
pre>
y< - c(3,4)
z < - x / y
zz
a < - 矩阵(x, nrow = 2,byrow = T)
b< - 矩阵(x,nrows = 2,byrow = T)
zxa
z * a
解决方案我假设您从GUI运行,错误并不致命。这是一个使用选项(错误)的解决方案。错误处理程序由增加变量的函数替换:
.error.count < - $ $ $ $ $ $ $ $ .error.fun< - getOption(error)
new.error.fun< - quote(.error.count< - .error.count + 1)
options(error = new .error.fun)
###你的代码在这里###
x< - c(1,2,3,4)
y< - c(3, (x,nrow = 2,byrow = T)
b < - 矩阵,byrow = T)
zxa
z * a
#####################
cat (ERROR COUNT:,.error.count,\\\
)
options(error = old.error.fun)
rm(.error.count,old.error.fun,new .error.fun)
Sometimes when I am running lots of long programs I think it would be nice if there was a statement or command I could add to the bottom of a file that would tell me whether R returned any error messages (or warning messages) while running a file.
I always scroll up through all of the code to visually check for error messages or warnings and keep thinking it would be nice if R simply told me at the bottom of the code whether any error messages or warnings occurred.
Can R do that? I suppose even if R can do that I would need a while to develop trust in the command line to catch all error messages or warning messages.
With SAS I used to use the find command and search the log window for the word ‘Error’ or ‘Warning’.
Thanks for any thoughts or advice about this.
Here is a very simple example of R code that returns 3 error messages.
x <- c(1,2,3,4) y <- c(3,4) z <- x / y zz a <- matrix(x, nrow=2, byrow=T) b <- matrix(x, nrows=2, byrow=T) z x a z * a
解决方案I assume you are running from a GUI, where errors are not fatal. Here is a solution making use of options(error). The error handler is replaced by a function that increments a variable:
.error.count <- 0 old.error.fun <- getOption("error") new.error.fun <- quote(.error.count <- .error.count + 1) options(error = new.error.fun) ### your code here ### x <- c(1,2,3,4) y <- c(3,4) z <- x / y zz a <- matrix(x, nrow=2, byrow=T) b <- matrix(x, nrows=2, byrow=T) z x a z * a ###################### cat("ERROR COUNT:", .error.count, "\n") options(error = old.error.fun) rm(.error.count, old.error.fun, new.error.fun)
这篇关于R:是否有一个命令结束一个文件,说明是否发生错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!