[R] aborting the execution of a function...

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Wed Jul 13 22:09:02 CEST 2022

Jeff & Akshay,

What you say is true if you just call a function and do not arrange to
handle various interrupts and errors.

Obviously if you find a way to gracefully handle an error then you can opt
to have your work so far saved. For what is meant to be a fatal error,
though, you are expected to GIVE UP or at least exit rapidly after doing
something graceful. 

I am not clear what machine the user is using and their R setup. It may be
you can find something like a try() or suspendInterrupts method to catch and
handle control-C but may I offer a DIFFERENT solution?

You can identify your R process and send it some other milder signal that
can easily be caught or change your loop to slow it down a bit more.

For example, change your code so it opens a file and writes data to it in
some format like one result per line or a CSV format or whatever makes you
happy. Your loop adds new lines/items to the file and perhaps even closes
the file on interrupt or whatever works. Or it writes the results to the
console where you can copy/paste from if not too long.

And, consider having long-running code periodically check something in the
environment looking for a signal. Say every hundredth iteration it checks
for the existence of a file called "STOP_IT.stupid" and if it sees it,
removes it and exits gracefully while preserving your results somehow or
whatever you need. No interrupt needed, just create an empty file or other
logical marker.

Another variant is to use some form of threading or subprocess that does the
work somewhat in the background but can get commands from the foreground
process as needed including a request to stop. Again, no horrible signals
that kill the program.

And note on some systems, a process can be halted and resumed, if the
problem is that it has run a long time and is using too many resources at a
time they are needed.

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Jeff Newmiller
Sent: Wednesday, July 13, 2022 3:49 PM
To: r-help using r-project.org; akshay kulkarni <akshay_e4 using hotmail.com>; R help
Mailing list <r-help using r-project.org>
Subject: Re: [R] aborting the execution of a function...

This would be easy for you to test on a small example on your local

But the answer is "no". Nothing is assigned if the function does not return
normally... and Ctrl+C is anything but normal.

On July 13, 2022 12:19:58 PM PDT, akshay kulkarni <akshay_e4 using hotmail.com>
>Dear members,
>                         I am running a large scraping code in a very
powerful AWS ec2 instance:
>DATES <- getFirms Dates()
>It iterates over 500 stocks from a website. Despite the power of the
machine, the execution is very slow.
>If I abort the function (by ctrl + C), after, say 150th iteration, the
DATES object will still contain the scraped data untill the 150th iteration,
right? ( The rest of the 350 entries will be NA's, I suppose).
>Many thanks in advance.....
>Yours sincerely,
>	[[alternative HTML version deleted]]
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>PLEASE do read the posting guide 
>and provide commented, minimal, self-contained, reproducible code.

Sent from my phone. Please excuse my brevity.

R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list