Jonathan Greenberg
2012-Oct-17 19:48 UTC
[R] Completely ignoring an error in a function...
The code base is a bit too complicated to paste in here, but the gist of my question is this: given I have a function myfunction <- function(x) { # Do something A # Do something B # Do something C } Say "#Do something B" returns this error: Error in cat(list(...), file, sep, fill, labels, append) : argument 2 (type 'list') cannot be handled by 'cat' A standard function would stop here. HOWEVER, I want, in this odd case, to say "keep going" to my function and have it proceeed to # Do something C. How do I accomplish this? I thought suppressWarnings() would do it but it doesn't appear to. Assume that debugging "Do something B" is out of the question. Why am I doing this? Because in my odd case, "Do something B" actually does what I needed it to, but returned an error that is irrelevant to my special case (it creates two files, failing on the second of the two files -- but the first file it creates is what I wanted and there is no current way to create that single file on its own without a lot of additional coding). --j -- Jonathan A. Greenberg, PhD Assistant Professor Department of Geography and Geographic Information Science University of Illinois at Urbana-Champaign 607 South Mathews Avenue, MC 150 Urbana, IL 61801 Phone: 217-300-1924 AIM: jgrn307, MSN: jgrn307@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 [[alternative HTML version deleted]]
I'd wrap it in try() - the failing function will run, and you can check after the fact whether it failed or not. Sarah On Wed, Oct 17, 2012 at 3:48 PM, Jonathan Greenberg <jgrn at illinois.edu> wrote:> The code base is a bit too complicated to paste in here, but the gist of my > question is this: given I have a function > > myfunction <- function(x) > { > # Do something A > # Do something B > # Do something C > } > > Say "#Do something B" returns this error: > Error in cat(list(...), file, sep, fill, labels, append) : > argument 2 (type 'list') cannot be handled by 'cat' > > A standard function would stop here. HOWEVER, I want, in this odd case, to > say "keep going" to my function and have it proceeed to # Do something C. > How do I accomplish this? I thought suppressWarnings() would do it but it > doesn't appear to. > > Assume that debugging "Do something B" is out of the question. Why am I > doing this? Because in my odd case, "Do something B" actually does what I > needed it to, but returned an error that is irrelevant to my special case > (it creates two files, failing on the second of the two files -- but the > first file it creates is what I wanted and there is no current way to > create that single file on its own without a lot of additional coding). > > --j-- Sarah Goslee http://www.functionaldiversity.org
Have a look at example(try) and demo(error.catching)> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Jonathan Greenberg > Sent: Wednesday, October 17, 2012 21:48 > To: r-help > Subject: [R] Completely ignoring an error in a function... > > The code base is a bit too complicated to paste in here, but the gist of my > question is this: given I have a function > > myfunction <- function(x) > { > # Do something A > # Do something B > # Do something C > } > > Say "#Do something B" returns this error: > Error in cat(list(...), file, sep, fill, labels, append) : > argument 2 (type 'list') cannot be handled by 'cat' > > A standard function would stop here. HOWEVER, I want, in this odd case, to > say "keep going" to my function and have it proceeed to # Do something C. > How do I accomplish this? I thought suppressWarnings() would do it but it > doesn't appear to. > > Assume that debugging "Do something B" is out of the question. Why am I > doing this? Because in my odd case, "Do something B" actually does what I > needed it to, but returned an error that is irrelevant to my special case (it > creates two files, failing on the second of the two files -- but the first file it > creates is what I wanted and there is no current way to create that single file > on its own without a lot of additional coding). > > --j > > -- > Jonathan A. Greenberg, PhD > Assistant Professor > Department of Geography and Geographic Information Science University of > Illinois at Urbana-Champaign > 607 South Mathews Avenue, MC 150 > Urbana, IL 61801 > Phone: 217-300-1924 > AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.