loops - How can I show the intermediate steps of a long routine in R? -


when run long routine in r, possible show intermediate steps?

for instance, i'm working routine building randomized versions of original matrix, based on null models (package bipartite):

#build n randomized version of matrix contained in data     nulls <- nullmodel(data, n=1000, method=3)  #calculate same network metric n randomized matrices modules.nulls <- sapply(nulls, computemodules, method = "beckett") 

depending on processing power of computer , size of n, takes long finish routine. include code show on console intermediate steps first , second parts of routine. "matrix 1, matrix 2... matrix n".

could please me? thank you!

1) cat can add cat, message or print statements function.

2) trace or if don't want modify function trace this:

# test function fun <- function(x) length(x)   trace(fun, quote(print(i <<- + 1)))  <- 0 out <- sapply(iris, fun) 

giving:

tracing fun(x[[i]], ...) on entry  [1] 1 tracing fun(x[[i]], ...) on entry  [1] 2 tracing fun(x[[i]], ...) on entry  [1] 3 tracing fun(x[[i]], ...) on entry  [1] 4 tracing fun(x[[i]], ...) on entry  [1] 5 

to reverse use untrace(fun) .

3) wrapper possibility create wrapper. flush.console optional , has effect of avoiding console buffering see output immediately.

wrap_fun <- function(x) { print(i <<- + 1); flush.console(); fun(x) }  <- 0 out <- sapply(iris, wrap_fun) 

4) tkprogressbar fancier approach use progress bar. sample code uses tcltk package included out-of-the-box in standard r distributions (so don't need download , install -- it's there , library statement sufficient load it).

library(tcltk)  fun2 <- function(x) sys.sleep(2) # test function wrap_fun2 <- function(x) {    <<- + 1    settkprogressbar(bar, i, label=i)    fun2(x) }  bar <- tkprogressbar("progress", max = 5) <- 0 out <- sapply(iris, wrap_fun2) close(bar) 

also see ?txtprogressbar , ?winprogressbar (windows only) , progress package other progress bars available.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -