不使用任务计划程序安排R脚本

问题描述:

我在Windows 7计算机上安装了RStudio,但是我无法访问Windows任务计划程序以自动执行一些R脚本。在这种情况下是否有其他选择?我探索了taskscheduleR但是发现这只是Task Scheduler的一个包装。使用VB脚本打开想法 - 基本上任何破解都可以做到。不使用任务计划程序安排R脚本

我会建议你的任务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