I am using certain functions from the fitdistrplus package as a part of a package I am creating.
I am attempting to prevent any error messages from displaying in the console when the functions are run and instead want to record the error messages in an error log that I am creating.
In most cases, using tryCatch() has allowed me to achieve this.
But specifically for the fitdist() function, I cannot suppress the error message being printed in the console even though the message is being written to the error log (meaning the tryCatch() expression is functioning).
I have replicated my issue in the code below.
library(fitdistrplus)
file.create("error_log.txt")
func_desc<-function(x){
tryCatch({
descdist(data = x)
},error = function(e){
write(x = paste(Sys.time(),"Error in func_desc :",e$message,sep = " "),file = "error_log.txt",append = T,sep = "\n")
})
}
func_fit<-function(x,dist){
tryCatch({
fitdist(data = x,distr = dist)
},error = function(e){
write(x = paste(Sys.time(),"Error in func_fit :",e$message,sep = " "),file = "error_log.txt",append = T,sep = "\n")
})
}
# Creating a vector of repeated values which will result in an error
test<-rep(x = 1,times = 10)
func_desc(x = test)
# Results in an error and the message is written to the error log and not printed in the console
func_fit(x = test,dist = "beta")
# Results in an error and the message is both written to the error log and printed in the console
I want to suppress this error message being printed by func_fit().
I have already tried the following alternatives:
try()withsilent = TRUE. The error message still gets printed.conditionMessage()gives the same result.withCallingHandlers()has been suggested in some posts and threads but I am unsure of how to implement it correctly.- Using
invisible()with the functions still prints the error.
.External2(C_optim, par, fn1, gr1, method, con, lower, upper)calls the C_optim function. Since i'm not exactly sure if the error has this origin, i'm not writing this as answer. This would explain why you can't mute it. (see here ) So either this works for you or another approach is (not recommended) rewriting the source code. - mischva11multicorepackage is no longer available on CRAN. Themulticorepage saysparallelcan be used as an alternative. I have gone through its documentation but I can't figure out how I can use it in my case. - Rage