I would like to use the 'tryCatch' function but am having a hard time getting my head around it. In 'C' like languages try/catch/finally means try a block of statements and if any throw an error then do the statements in the catch block and then error or not always do the staements in the finally block. In 'R' as best as I can tell the block of staements in the try block is a single function. Does this mean I need to construct a temporary function that contains what I would have put in the try blck? Also in 'R' it seems the 'finally' function (of 'tryCatch' most closely mimics the catch block of 'C'. Right? A similar comment on the finally function. Do I need to create a temporary function that contains the staements that I normally would put in a 'C' like catch clause? If I do need to create functions for the try and error conditions what is the scope or enviironment that the functions are called under? The 'try' function is relatively straightforward but when an error occurs what variable will I have access to? Thank you. Kevin
Read the argment descriptions and Look at the examples in ?tryCatch. The `expr' argument (i.e. the code to try) and the `finally' argument are expressions that are evaluated (via standard lazy evaluation of arguments). The error condition handlers, provided as the `...' argument in errorClass = handler form(s), are functions of one argument, the error condition. As in the examples I usualy use fuctions writen in-line. There is no non-standard evaluation involved, so standard scoping rules apply. luke On Thu, 14 Aug 2008, rkevinburton at charter.net wrote:> I would like to use the 'tryCatch' function but am having a hardtime getting my head around it. In 'C' like languages try/catch/finally means try a block of statements and if any throw an error then do the statements in the catch block and then error or not always do the staements in the finally block. In 'R' as best as I can tell the block of staements in the try block is a single function. Does this mean I need to construct a temporary function that contains what I would have put in the try blck? Also in 'R' it seems the 'finally' function (of 'tryCatch' most closely mimics the catch block of 'C'. Right? A similar comment on the finally function. Do I need to create a temporary function that contains the staements that I normally would put in a 'C' like catch clause? If I do need to create functions for the try and error conditions what is the scope or enviironment that the functions are called under? The 'try' function is relatively straightforward but when an error occurs what variable will I have access to?> > Thank you. > > Kevin > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Luke Tierney Chair, Statistics and Actuarial Science Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke at stat.uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
Hi Kevin, I learned to use tryCatch() from this page: http://www1.maths.lth.se/help/R/ExceptionHandlingInR/ Hope this helps, ST ----- Original Message ---- From: Luke Tierney <luke at stat.uiowa.edu> To: rkevinburton at charter.net Cc: r-help at r-project.org Sent: Thursday, August 14, 2008 8:59:35 AM Subject: Re: [R] tryCatch question Read the argment descriptions and Look at the examples in ?tryCatch. The `expr' argument (i.e. the code to try) and the `finally' argument are expressions that are evaluated (via standard lazy evaluation of arguments). The error condition handlers, provided as the `...' argument in errorClass = handler form(s), are functions of one argument, the error condition. As in the examples I usualy use fuctions writen in-line. There is no non-standard evaluation involved, so standard scoping rules apply. luke On Thu, 14 Aug 2008, rkevinburton at charter.net wrote:> I would like to use the 'tryCatch' function but am having a hardtime getting my head around it. In 'C' like languages try/catch/finally means try a block of statements and if any throw an error then do the statements in the catch block and then error or not always do the staements in the finally block. In 'R' as best as I can tell the block of staements in the try block is a single function. Does this mean I need to construct a temporary function that contains what I would have put in the try blck? Also in 'R' it seems the 'finally' function (of 'tryCatch' most closely mimics the catch block of 'C'. Right? A similar comment on the finally function. Do I need to create a temporary function that contains the staements that I normally would put in a 'C' like catch clause? If I do need to create functions for the try and error conditions what is the scope or enviironment that the functions are called under? The 'try' function is relatively straightforward but when an error occurs what variable will I have access to?> > Thank you. > > Kevin > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Luke Tierney Chair, Statistics and Actuarial Science Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke at stat.uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi there, I have a nested call of lapply in which I do a tryCatch statement like this lapply(1:length(p_names), function(w_idx) { r <- as.numeric(pos_r[[w_idx]][1]) c <- as.numeric(pos_c[[w_idx]][1]) pos <- c(r,c) lapply(1:length(p_names[[w_idx]]), function(p_idx, pos) { parameter <- p_names[[w_idx]][[p_idx]] value <- p_vals[[w_idx]][[p_idx]] tryCatch({ as.numeric(value) }, warning = function(ex) { value}) oad$resultValues[[parameter]][pos[1],pos[2]] <<- value NULL }, pos) }) (sorry, that I don't have a simple test code here...) the tryCatch shall convert a value in a number if this makes sense (so "3.3" should be converted, but not "hello"; all values are strings) if I simply execute something like this: value <- "3.3" tryCatch({ as.numeric(value) }, warning = function(ex) { value}) it works pretty nice. but within the lapply construct I always end up with strings. Does anybody have an idea why? (I can try to create a simple testcode for this but maybe there is already someone who knows what's wrong? Ciao, Antje