Question

I am using mapply(function,args), for a big dataset. After 100 iterations I need to set a delay for 1 sec. So the question is if it possible to show iteration count or progress bar within mapply (function, args) Thanks

Était-ce utile?

La solution

No, but if you switch to using the corresponding functions from plyr you can add a progress bar to the function call.

Without you giving us a minimal, reproducible example I'm not going to the effort of finding the exact plyr equivalent, but it will be one of the m*ply functions:

> ls(pos=2,pattern="m.*ply")
[1] "maply" "mdply" "mlply" "m_ply"

Autres conseils

If you know the total number of iterations in advance, you could just add another argument to mapply as an iteration counter. In this example I added z. This example makes the command line sleep for 1 second every 3 iterations....

mapply( function(x,y,z) { if(z%%3==0){Sys.sleep(1);
    cat(paste0( "Interation " , z , " ...sleeping\n") ) }
    x*y } ,x=1:10,y=1:10,z=1:10)
#Interation 3 ...sleeping
#Interation 6 ...sleeping
#Interation 9 ...sleeping
# [1]   1   4   9  16  25  36  49  64  81 100

If you need more convincing wrap the statement in system.time(). I get a runtime of 3.002 seconds.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top