0

I am trying to use tryCatch() in a linux version of R following a recommendation to a previous post (see Linux Server crash in R parallel - Error in unserialize(node$con) : error reading from connection). Basically my code is in the following structure:

Load libraries    
Set some input parameters
ans <- DEoptim(Calibrate,lower,upper,DEoptim.control(trace=TRUE,parallelType=1,parVar=c(parVarnames),packages=c("raster","rgdal","maptools","Cairo")))
Calibrate is a function that calls lots of other functions
At the end it returns a variable (-Nashsutcliffe) to DEoptim

This code is ran in parallel at continues to try different input parameters in the function Calibrate and DEoptim tries to find the best set of input parameters to maximise Nashsutcliffe (the objective function). Although the code runs fine from anywhere between a couple of minutes to a couple of hours, eventually it fails showing:

Error in unserialize(node$con) : error reading from connection

So how would I incorporate tryCatch() into the code above to catch the error. I have looked at the documentation but don't understand how to use it. Any help would be appreciated (suggested by Roman Luštrik)?

Many thanks

Antony Walker

Community
  • 1
  • 1
AntonyDW
  • 349
  • 5
  • 17
  • 1
    This might be duplicated but I really don't understand where to put the various tryCatch element within the code above. – AntonyDW Nov 16 '15 at 11:17
  • I only see one line of code above. And of course, you'd need to wrap the whole line in a `tryCatch` statement. – Roland Nov 16 '15 at 12:29
  • Sorry, there are approximately 2000 lines of code but all driven from ans <- DEoptim etc which calls the calibrate function which then calls other functions etc etc. Where would I put the tryCatch code and what else would I need. I just don't get it. – AntonyDW Nov 16 '15 at 12:49
  • We neither know your code nor what exactly you don't get. The principle of using `tryCatch` is explained in the linked question and shouldn't be too hard to understand. I suggest that you first do some experiments using `tryCatch` in a simplified example. – Roland Nov 16 '15 at 12:53
  • Could I send you my code and you could give advice? I really don't understand this one even with examples - it looks to me like you are forcing the system to give an error message - but I just want to know why it is crashing! – AntonyDW Nov 16 '15 at 14:34
  • I think it is very possibly a resource issue as it always runs fine for the first few runs. I need to find out how to rm(list=ls()) for each worker at the beginning of each run (and not clear all the variables for all workers). I'll abandon tryCatch - thanks to all. – AntonyDW Nov 16 '15 at 19:09

0 Answers0