How to make execution pause, sleep, wait for X seconds in R?
See help(Sys.sleep)
.
For example, from ?Sys.sleep
testit <- function(x)
{
p1 <- proc.time()
Sys.sleep(x)
proc.time() - p1 # The cpu usage should be negligible
}
testit(3.7)
Yielding
> testit(3.7)
user system elapsed
0.000 0.000 3.704
Is there a pause function in R?
Yes, you can pause execution by using Sys.sleep()
. So, to wait for one second: Sys.sleep(1)
.
how to pause an R script until a file has been downloaded?
actually found a solution from this question
Wait for file to exist
library(reticulate)
source_python("C:/Users/Gunathilakel/Desktop/Dashboard Project/Dashboard/PBK-Report-Automation-for-Dashboard-master/pbk automation.py")
while (!file.exists("C:/Users/Gunathilakel/Downloads/Issued and Referral Charge.csv")) {
Sys.sleep(1)
}
# Moves Downloaded CSV file of Issued_and_refused from Downloads --------
my.file.copy <- function(from, to) {
todir <- dirname(to)
if (!isTRUE(file.info(todir)$isdir)) dir.create(todir, recursive=TRUE)
file.copy(from = from, to = to,overwrite = TRUE)
}
my.file.copy(from = "C:/Users/Gunathilakel/Downloads/Issued and Referral Charge.csv",
to = "C:/Users/Gunathilakel/Desktop/Dashboard Project/Dashboard/RawData/Issued and Referral Charge2020.csv")
R: Offer 5 seconds to demand a pause. If no pause demanded, resume the process
Here is a quick little function based on the tcltk and tcltk2 packages:
library(tcltk)
library(tcltk2)
mywait <- function() {
tt <- tktoplevel()
tmp <- tclAfter(5000, function()tkdestroy(tt))
tkpack( tkbutton(tt, text='Pause', command=function()tclAfterCancel(tmp)))
tkpack( tkbutton(tt, text='Continue', command=function()tkdestroy(tt)),
side='bottom')
tkbind(tt,'<Key>', function()tkdestroy(tt) )
tkwait.window(tt)
invisible()
}
Run mywait
and a small window will pop-up with 2 buttons, if you don't do anything then after about 5 seconds the window will go away and mywait
will return allowing R to continue. If you click on "Continue" at any time then it will return immediately. If you click on "Pause" then the countdown will stop and it will wait for you to click on continue (or pressing an key) before continuing on.
This is an extension of the answer given here.
Plot progressively using system pause/sleep
This is how it worked:
png(file="kMeanPlot%03d.png", width=719, height=539)
for(i in seq(3, nrow(pima_diabetes_kmean), by = 20)){
k.means.fit <- kmeans(pima_diabetes_kmean[1:i, c("Pregnancy", "Age")], 2)
plot(
pima_diabetes_kmean[1:i, c("Pregnancy", "Age")],
col = alpha(k.means.fit$cluster, 0.2),
pch = 20,
cex = 3, main = "Clusters predicted by models"
)
points(
k.means.fit$centers,
pch = 4,
cex = 4,
lwd = 4,
col = "blue"
)
}
dev.off()
# convert the .png files to one .gif file using ImageMagick.
# The system()/shell() function executes the command as if it was done
# in the terminal. the -delay flag sets the time between showing
# the frames, i.e. the speed of the animation.
# You will need to install the imagemagick for this
# Following command for non-windows OS
# system("C:/Program Files/ImageMagick-7.0.5-Q16/convert -delay 80 *.png fucked.gif")
# Following command for Windows OS
shell("convert -delay 40 *.png kMeanPlot.gif")
# to not leave the directory with the single png files I remove them.
file.remove(list.files(pattern="[kMeanPlot]*.png"))
Execute for-loop at fixed time intervals, E.,g. get plots every 5 seconds
You can wait for user input before showing the next plot:
# Wait for user input before showing next plot
par(ask=TRUE)
# Loop that makes plots
for(i in 1:10) {
plot(rnorm(20))
}
To actually wait for 5 seconds between plots:
# Loop that makes plots
for(i in 1:10) {
plot(rnorm(20))
# Wait 5 seconds
Sys.sleep(5)
}
Related Topics
Subset Rows in a Data Frame Based on a Vector of Values
Multiply Rows of Matrix by Vector
Finding Rows Containing a Value (Or Values) in Any Column
How to Calculate Cumulative Sum
Fitting Several Regression Models With Dplyr
Remove Na Values from a Vector
Control Ggplot2 Legend Look Without Affecting the Plot
How to Put Labels Over Geom_Bar For Each Bar in R With Ggplot2
Custom Legend For Multiple Layer Ggplot
How to Get Week Numbers from Dates
Find How Many Times Duplicated Rows Repeat in R Data Frame
Count the Number of All Words in a String
R: Gsub, Pattern = Vector and Replacement = Vector
Define and Apply Custom Bins on a Dataframe
How to Omit Na Values While Pasting Numerous Column Values Together