2012-04-18 22 views
4

必要な特定の時刻にRコードを実行したい。 プロセスが終了した後、私はRセッションを終了したいと思います。特定の時刻にRコードを実行したい

コードは以下の通りであるならば、

tm<-Sys.time() 
write.table(tm,file='OUT.TXT', sep='\t'); 
quit(save = "no") 

が、私は「2012年4月18日17時25分40秒」でこのコードを実行するために何をすべき。 私はあなたの助けが必要です。前もって感謝します。

+1

Rの外で 'cron'を使用したと考えましたか?いくつかのリード:[Linuxの](http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/)の[cronの使用](http://www.scrounge.org/linux/cron.html)、[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

+0

私はタスクスケジューラとバッチファイルを使ってこの問題を解決しました。ありがとう:) –

答えて

11

Windowsの場合はTask Scheduler、またはLinuxの場合はcron jobを使用するのが最も簡単です。指定した特定の時刻に実行するコマンドまたはプログラムを指定できます。私は間違いなくないは次のようにRスクリプトをお勧めします:どういうわけかあなたはcronジョブサービスを使用して、R、以下の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 
    } 
} 
+2

あなたは 'else {print("私はまだ待っています... \ n ")}' :-) –

+0

私はタスクスケジューラとバッチファイルを使ってこの問題を解決しました。ありがとう:) –

4

ように、特定の時間を待つ方法を示しています予め指定された目標時間に実行する。

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 
    } 
    } 
} 
+0

輝き、ただ普通の輝き – emilBeBri

関連する問題