Dear Jeff, Thanks! I think it is an idiosyncrasy of tryCatch? The other arguments like "error" doesn't need to be assigned to a call right? Just the definition would be sufficient, i think? Yours sincerely, AKSHAY M KULKARNI ________________________________ From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us> Sent: Thursday, June 23, 2022 12:53 AM To: r-help at r-project.org <r-help at r-project.org>; akshay kulkarni <akshay_e4 at hotmail.com>; R help Mailing list <r-help at r-project.org> Subject: Re: [R] inconsistency in tryCatch... You defined a function. You did not call the function. tryCatch returned the object you defined. So the interactive console printed the object returned. Invoking the "function" function does not call the defined function for you. Try: tryCatch((function() print("fred"))(), error = function(e) sum(1:3), finally = sum(1:3)) On June 22, 2022 12:00:38 PM PDT, akshay kulkarni <akshay_e4 at hotmail.com> wrote:>Dear members, > I have the following code: > > > tryCatch(function() print("fred"), error = function(e) sum(1:3), finally = sum(1:3)) > function() print("fred") > >The expected output from the tryCatch call should be to print "fred" to the console, and exit, but as seen above, it is outputting > function() print("fred") > >Can you people please shed some light on what is happening? > >thanking you, >Yours sincerely, >AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.-- Sent from my phone. Please excuse my brevity. [[alternative HTML version deleted]]
I don't think it is at all idiosyncratic... tryCatch doesn't expect the first argument to be a function... it is supposed to be the actual code that might break and raise an error. There are lots of functions (like lapply) that do expect you to provide a function, and even the other parameters to tryCatch expect a function, but you would not write a for loop and have a function definition in the body of the loop and expect the for loop to know it was supposed to call that function, would you? Think of tryCatch like a for loop or an if statement. On June 22, 2022 12:36:27 PM PDT, akshay kulkarni <akshay_e4 at hotmail.com> wrote:>Dear Jeff, > Thanks! I think it is an idiosyncrasy of tryCatch? The other arguments like "error" doesn't need to be assigned to a call right? Just the definition would be sufficient, i think? > >Yours sincerely, >AKSHAY M KULKARNI >________________________________ >From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us> >Sent: Thursday, June 23, 2022 12:53 AM >To: r-help at r-project.org <r-help at r-project.org>; akshay kulkarni <akshay_e4 at hotmail.com>; R help Mailing list <r-help at r-project.org> >Subject: Re: [R] inconsistency in tryCatch... > >You defined a function. You did not call the function. tryCatch returned the object you defined. So the interactive console printed the object returned. > >Invoking the "function" function does not call the defined function for you. Try: > >tryCatch((function() print("fred"))(), error = function(e) sum(1:3), finally = sum(1:3)) > >On June 22, 2022 12:00:38 PM PDT, akshay kulkarni <akshay_e4 at hotmail.com> wrote: >>Dear members, >> I have the following code: >> >> > tryCatch(function() print("fred"), error = function(e) sum(1:3), finally = sum(1:3)) >> function() print("fred") >> >>The expected output from the tryCatch call should be to print "fred" to the console, and exit, but as seen above, it is outputting >> function() print("fred") >> >>Can you people please shed some light on what is happening? >> >>thanking you, >>Yours sincerely, >>AKSHAY M KULKARNI >> >> [[alternative HTML version deleted]] >> >>______________________________________________ >>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>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. > >-- >Sent from my phone. Please excuse my brevity.-- Sent from my phone. Please excuse my brevity.
I think the confusion is simple. What you *really* wanted to do was tryCatch(print("fred"), -- other stuff --) because the first argument of tryCatch is an EXPRESSION. You don't need to have 'function' in there anywhere at all. Every function in R gets to decide for itself which, if any, of its arguments are evaluated, and when. NONE of the arguments of tryCatch is evaluated until tryCatch decides it is time. In most other programming languages, arguments are evaluated before the call, and if you want something delayed, you have to wrap it in an anonymous function. Not R.d So tryCatch - first sets up condition handling - THEN evaluates its first argument On Thu, 23 Jun 2022 at 07:42, akshay kulkarni <akshay_e4 at hotmail.com> wrote:> Dear Jeff, > Thanks! I think it is an idiosyncrasy of tryCatch? The > other arguments like "error" doesn't need to be assigned to a call right? > Just the definition would be sufficient, i think? > > Yours sincerely, > AKSHAY M KULKARNI > ________________________________ > From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us> > Sent: Thursday, June 23, 2022 12:53 AM > To: r-help at r-project.org <r-help at r-project.org>; akshay kulkarni < > akshay_e4 at hotmail.com>; R help Mailing list <r-help at r-project.org> > Subject: Re: [R] inconsistency in tryCatch... > > You defined a function. You did not call the function. tryCatch returned > the object you defined. So the interactive console printed the object > returned. > > Invoking the "function" function does not call the defined function for > you. Try: > > tryCatch((function() print("fred"))(), error = function(e) sum(1:3), > finally = sum(1:3)) > > On June 22, 2022 12:00:38 PM PDT, akshay kulkarni <akshay_e4 at hotmail.com> > wrote: > >Dear members, > > I have the following code: > > > > > tryCatch(function() print("fred"), error = function(e) > sum(1:3), finally = sum(1:3)) > > function() print("fred") > > > >The expected output from the tryCatch call should be to print "fred" to > the console, and exit, but as seen above, it is outputting > > function() print("fred") > > > >Can you people please shed some light on what is happening? > > > >thanking you, > >Yours sincerely, > >AKSHAY M KULKARNI > > > > [[alternative HTML version deleted]] > > > >______________________________________________ > >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >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. > > -- > Sent from my phone. Please excuse my brevity. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]