In general, reporting the progress of a function will require a loop (or possibly a number of lines of code), as you've set out in your example. Your example is performing the calculations, it is just not returning them - remember that R functions return the value of the last calculation unless there is an explicit return()
statement. In this case, the message()
function returns NULL
.
The minor modification to your code below demonstrates the expected behaviour of the progress bar, followed by result
having the calculated value.
install.packages("svMisc")
require("svMisc")
# Test function
funct<-function(a,b,c){
for (i in 0:101){
progress(i, progress.bar=TRUE)
Sys.sleep(0.01)
x<-a*a
y<-x+b
z<-y/2
if (i == 101) message("Done!")
}
return(z)
}
result <- funct(-2.6e+70,-2.5e+121,6)
# 0%---------25%---------50%---------75%--------100%
# Progress: ||||||||||||||||||||||||||||||||||||||||||||||||||
# Done!
result
# [1] 3.38e+140
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…