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

6
  • 1
    This might be duplicated but I really don't understand where to put the various tryCatch element within the code above. Commented Nov 16, 2015 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. Commented Nov 16, 2015 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. Commented Nov 16, 2015 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. Commented Nov 16, 2015 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! Commented Nov 16, 2015 at 14:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.