如何在R中打印实际的错误消息
我试图在R函数中返回任何实际的错误消息,并向其发送电子邮件通知用户,但它仅在R参数中打印自定义消息。有没有办法在电子邮件中发送实际的错误消息 以下是我迄今为止编写的虚拟脚本:如何在R中打印实际的错误消息,r,error-handling,try-catch,R,Error Handling,Try Catch,我试图在R函数中返回任何实际的错误消息,并向其发送电子邮件通知用户,但它仅在R参数中打印自定义消息。有没有办法在电子邮件中发送实际的错误消息 以下是我迄今为止编写的虚拟脚本: mailme <- function(message){ #function to send email } b<-function(){ r <- NULL attempt <- 1 while( is.null(r) && attempt <= 3 ) {
mailme <- function(message){
#function to send email
}
b<-function(){
r <- NULL
attempt <- 1
while( is.null(r) && attempt <= 3 ) {
attempt <- attempt + 1
try({
x<-2+3
prin(x)})
}
stop("The error message")
}
a <- tryCatch({
b()
}, error = function(e){
mailme(e$message)
})
但是,我在电子邮件中收到的错误消息是
The error message #from the stop used in function b
如何调用站点内的实际错误消息?我可能误解了你的问题,但我觉得你用两个trycatch不必要地把事情复杂化了。为什么不定义函数,然后在一个tryCatch中调用它呢。一个更简单的例子:
# function that will always error
my_fun <- function() prin(x)
tryCatch({
my_fun()
}, error = function(e) {
print(e$message) # replace with mailme(e$message)
})
# [1] "could not find function \"prin\""
当然,在您的实际代码中用mailme替换print。您好,非常感谢您的回复,我试图实现的功能是,在您的示例中,my_fun本身应该在失败后重试一次,然后再抛出错误消息
# function that will always error
my_fun <- function() prin(x)
tryCatch({
my_fun()
}, error = function(e) {
print(e$message) # replace with mailme(e$message)
})
# [1] "could not find function \"prin\""