Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: include a function decorator that works with progressr #122

Open
tanho63 opened this issue Aug 13, 2021 · 0 comments
Open

Suggestion: include a function decorator that works with progressr #122

tanho63 opened this issue Aug 13, 2021 · 0 comments

Comments

@tanho63
Copy link

tanho63 commented Aug 13, 2021

I was wondering if it would make sense to have a function decorator explicitly for (simple) progress updates, so that I can extend the behaviour of pre-existing functions - something like:

progressively <- function(f, p = NULL){
  if(is.null(p)) p <- function(...) NULL
  force(f)

  function(...){
    on.exit(p("loading..."))
    f(...)
  }

}

which then could be called like:

read_rosters <- function(){
  urls <- c("https://github.com/nflverse/nflfastR-roster/raw/master/data/seasons/roster_2020.csv", "https://github.com/nflverse/nflfastR-roster/raw/master/data/seasons/roster_2021.csv")
  p <- progressor(along = urls)
  purrr::map_dfr(urls, progressively(read.csv, p))
}

x <- with_progress(read_rosters())

This is inspired by Hadley's notes on function operators in Advanced R, and purrr's quietly/safely set of decorators

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant