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

akshay kulkarni @k@h@y_e4 @end|ng |rom hotm@||@com
Thu Jul 14 18:09:54 CEST 2022


Dear Bill,
                Many thanks ......

Yours sincrely,
AKSHAY M KULKARNI
________________________________
From: Bill Dunlap <williamwdunlap using gmail.com>
Sent: Thursday, July 14, 2022 1:28 AM
To: akshay kulkarni <akshay_e4 using hotmail.com>
Cc: R help Mailing list <r-help using r-project.org>
Subject: Re: [R] aborting the execution of a function...

You could write a function that returns an environment (or list if you prefer) containing the results collected before the interrupt by using tryCatch(interrupt=...).  E.g.,

doMany <- function(names) {
    resultEnv <- new.env(parent=emptyenv())
    tryCatch(
        for(name in names) resultEnv[[name]] <- Sys.sleep(1), # replace Sys.sleep(1) by getStuffFromWeb(name)
        interrupt = function(e) NULL)
    resultEnv
}

Use it as

> system.time(e <- doMany(state.name<http://state.name>)) # hit Esc or ^C after a few seconds
^C   user  system elapsed
  0.001   0.000   4.390
> names(e)
[1] "Alabama"  "Alaska"   "Arizona"  "Arkansas"
> eapply(e, identity)
$Alabama
NULL

$Alaska
NULL

$Arizona
NULL

$Arkansas
NULL

-Bill

On Wed, Jul 13, 2022 at 12:20 PM akshay kulkarni <akshay_e4 using hotmail.com<mailto:akshay_e4 using hotmail.com>> wrote:
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,
AKSHAY M KULKARNI



        [[alternative HTML version deleted]]

______________________________________________
R-help using r-project.org<mailto:R-help using 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]]



More information about the R-help mailing list