Skip to content

Instantly share code, notes, and snippets.

@johnbaums
Last active May 27, 2019 01:27
Show Gist options
  • Save johnbaums/560058f7bc2782c8ffbf5eede35ca1fb to your computer and use it in GitHub Desktop.
Save johnbaums/560058f7bc2782c8ffbf5eede35ca1fb to your computer and use it in GitHub Desktop.
sapply with progress text (percent complete)
pbsapply <- function(x, fun, pass_to, ...) {
if(!missing('pass_to')) {
call <- sprintf('fun(%s=x[[i]], ...)', pass_to)
} else {
call <- 'fun(x[[1]], ...) '
}
sapply(seq_along(x), function(i) {
cat(sprintf('\r%.02f%%', i/length(x)*100))
eval(parse(text=call))
})
}
@johnbaums
Copy link
Author

pbsapply(runif(5), Sys.sleep, pass_to='time')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment