不使用 Task Scheduler 安排 R 脚本
Schedule R scripts without using Task Scheduler
我在我的 Windows 7 机器上安装了 RStudio,但是我无法访问 Windows Task Scheduler 来自动化一些 R 脚本。在这种情况下还有其他选择吗?我探索了 taskscheduleR
,但发现这只是 Task Scheduler 的包装器。接受使用 VB 脚本的想法 - 基本上任何 hack 都可以。
我建议 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
每当您想停止 运行ning 这个宏时,您 运行 这个;
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
您可以像这样从立即数 window 调用宏 Start
:
Call Start(TimeValue("14:00:00 pm"))
并且您的宏将 运行 在 2 p.m。每天。
我在我的 Windows 7 机器上安装了 RStudio,但是我无法访问 Windows Task Scheduler 来自动化一些 R 脚本。在这种情况下还有其他选择吗?我探索了 taskscheduleR
,但发现这只是 Task Scheduler 的包装器。接受使用 VB 脚本的想法 - 基本上任何 hack 都可以。
我建议 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
每当您想停止 运行ning 这个宏时,您 运行 这个;
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
您可以像这样从立即数 window 调用宏 Start
:
Call Start(TimeValue("14:00:00 pm"))
并且您的宏将 运行 在 2 p.m。每天。