2017-06-05 57 views
1

我在Windows 7計算機上安裝了RStudio,但是我無法訪問Windows任務計劃程序以自動執行一些R腳本。在這種情況下是否有其他選擇?我探索了taskscheduleR但是發現這只是Task Scheduler的一個包裝。使用VB腳本打開想法 - 基本上任何破解都可以做到。不使用任務計劃程序安排R腳本

回答

0

我會建議你的任務VBA,你需要學習如何使用Application.OnTime;

你需要其他潛艇來觸發和停止這個你想要的;第一個可以開始時間表。

Public stop_running As Boolean 
Public sTime As String 

Sub Start(input As String) 

    stop_running = True 'This controls schedule in Application.Ontime 
    sTime = input 
    Application.OnTime TimeValue(sTime), "Rscript", Schedule:=stop_running 

End Sub 

每當你想停止運行這個宏,你運行這個;

Sub Finish() 

    stop_running = False 'Set the schedule to false to stop the macro 
    Application.OnTime TimeValue(sTime), "Rscript", Schedule:=stop_running 

End Sub 

那麼這是一個運行在您的R-腳本子:

Sub Rscript() 
'runs R script through cmd 

    If Not stop_running Exit Sub 

Dim shell As Object 
Set shell = VBA.CreateObject("WScript.Shell") 
Dim waitTillComplete As Boolean: waitTillComplete = True 
Dim style As Integer: style = 1 
Dim errorCode As Integer 
Dim path As String 

path = "RScript C:\R\myscript.R" 
errorCode = shell.Run(path, style, waitTillComplete) 

End Sub 

您可以從即時窗口調用宏Start這樣的:

Call Start(TimeValue("14:00:00 pm")) 

而且你的宏將運行在下午2點每天。

+0

我該如何讓它每天在下午2點運行而無需干預? –

+0

@ScottHorvath更新了答案。 – Masoud

相關問題