tidyverse / purrr

A functional programming toolkit for R

Home Page:https://purrr.tidyverse.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The case for a `quickly()` adverb function

jl5000 opened this issue · comments

I think it would be useful to have an adverb function quickly() which will limit the amount of time spent trying to execute a function. If a given time is exceeded, it can either throw an error, warning, or move on silently.

I am currently using something like this when calling a function for its side effects but am dealing with slow/unreliable network connections. Perhaps for functions that return values, a default value could be defined.

I'm currently doing something like this:

do_it_fast <- function(){
  success <- FALSE
  tryCatch({
    
    time_limit <- 2
    
    setTimeLimit(cpu = time_limit, elapsed = time_limit, transient = TRUE)
    
    # Do stuff
    success <- TRUE
    
  }, error = function(e) {
  }, warning = function(w) {
  }, finally = {
    setTimeLimit(cpu = Inf, elapsed = Inf, transient = FALSE)
  })
  success
}

setTimeLimit() only applies to top-level computations, so I don't think it's possible to implement the function that you want.