Skip to content

Instantly share code, notes, and snippets.

@bedantaguru
Last active September 1, 2017 06:43
Show Gist options
  • Save bedantaguru/1004cd27774436ca334ee0d44569d918 to your computer and use it in GitHub Desktop.
Save bedantaguru/1004cd27774436ca334ee0d44569d918 to your computer and use it in GitHub Desktop.
Defining Variable in R Package Environment
progress_print <- function(){
L <- list(init = function(x) {M <<- x}, step = function() NULL, term = function() NULL)
itr <- 0
M <- 1
L$step <- function(){
itr <<- itr + 1
cat(paste0("Job ", itr, " out of ", M, "\n"))
}
return(L)
}
# this will give error
assign("progress_print", value = progress_print, envir = environment(llply))
# so we have to do like this
environment(progress_print) <- environment(llply)
# here is one usage of above
ldply(seq(10), function(i) data.frame(x = rnorm(1), y = rnorm(1)), .progress = "print")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment