无需使用任务计划程序即可安排R脚本

时间:2017-06-05 21:07:15

标签: r vba excel-vba vbscript taskscheduler

我在Windows 7计算机上安装了RStudio但是我无法访问Windows任务计划程序来自动执行某些R脚本。在这种情况下还有其他选择吗?我已经探索过taskscheduleR然而发现这只是Task Scheduler的包装器。使用VB脚本开放思想 - 基本上任何黑客都可以。

1 个答案:

答案 0 :(得分: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点运行。日常。