我想在我需要的特定時間運行R代碼。 並且在處理完成後,我想終止R會話。我想在特定時間運行R代碼
如果代碼是如下,
tm<-Sys.time()
write.table(tm,file='OUT.TXT', sep='\t');
quit(save = "no")
我應該怎麼做才能運行在「2012-04-18十七時25分40秒」這個代碼。 我需要你的幫助。提前致謝。
我想在我需要的特定時間運行R代碼。 並且在處理完成後,我想終止R會話。我想在特定時間運行R代碼
如果代碼是如下,
tm<-Sys.time()
write.table(tm,file='OUT.TXT', sep='\t');
quit(save = "no")
我應該怎麼做才能運行在「2012-04-18十七時25分40秒」這個代碼。 我需要你的幫助。提前致謝。
在Linux下最容易使用Windows的Task Scheduler或cron job。在那裏你可以指定一個應該在你指定的特定時間運行的命令或程序。我肯定會不建議喜歡的R腳本:
time_to_run = as.POSIXct("2012-04-18 17:25:40")
while(TRUE) {
Sys.sleep(1)
if(Sys.time == time_to_run) {
## run some code
}
}
您遺漏了'else {print(「我還在等待... \ n」)}':-) – 2012-04-18 12:04:40
我使用任務計劃程序和批處理文件解決了此問題。感謝:) – 2012-04-19 05:12:08
如果不知爲何,你不能使用cron作業服務,並有R內安排,下列R-代碼演示如何等待特定的時間量,從而在預先指定的目標時間執行。
stop.date.time.1 <- as.POSIXct("2012-12-20 13:45:00 EST") # time of last afternoon execution.
stop.date.time.2 <- as.POSIXct("2012-12-20 7:45:00 EST") # time of last morning execution.
NOW <- Sys.time() # the current time
lapse.time <- 24 * 60 * 60 # A day's worth of time in Seconds
all.exec.times.1 <- seq(stop.date.time.1, NOW, -lapse.time) # all of afternoon execution times.
all.exec.times.2 <- seq(stop.date.time.2, NOW, -lapse.time) # all of morning execution times.
all.exec.times <- sort(c(all.exec.times.1, all.exec.times.2)) # combine all times and sort from recent to future
cat("To execute your code at the following times:\n"); print(all.exec.times)
for (i in seq(length(all.exec.times))) { # for each target time in the sequence
## How long do I have to wait for the next execution from Now.
wait.time <- difftime(Sys.time(), all.exec.times[i], units="secs") # calc difference in seconds.
cat("Waiting for", wait.time, "seconds before next execution\n")
if (wait.time > 0) {
Sys.sleep(wait.time) # Wait from Now until the target time arrives (for "wait.time" seconds)
{
## Put your execution code or function call here
}
}
}
輝煌,只是平淡的光輝 – emilBeBri 2017-09-05 23:56:25
你有沒有考慮過去R之外並使用'cron'?一些導致:[使用cron](http://www.scrounge.org/linux/cron.html),[Linux](http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/),[Windows cron等效](http://stackoverflow.com/questions/132971/what-is-the-windows-version-of-cron)或[OSX](http://superuser.com/questions/243893/how-to -make-run-cron-on-osx-10-6-2) – gauden 2012-04-18 07:29:28
我使用任務調度程序和批處理文件解決了這個問題。謝謝:) – 2012-04-19 05:10:55