调度 R 脚本
我编写了一个 R 脚本,它从数据库中提取一些数据,对其执行多项操作并将输出发布到新数据库。
我希望这个脚本每天在特定时间运行,但我找不到任何方法来有效地执行此操作。
谁能推荐我可以查看的资源来解决这个问题?我正在 Windows 计算机上运行此脚本。
I have written an R script that pulls some data from a database, performs several operations on it and post the output to a new database.
I would like this script to run every day at a specific time but I can not find any way to do this effectively.
Can anyone recommend a resource I could look at to solve this issue? I am running this script on a Windows machine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
实际上,在 Windows 下,您甚至不必先创建批处理文件即可使用调度程序。
“C:\Program Files\R\R-3.0.2\bin\x64\Rscript.exe”
Actually under Windows you do not even have to create a batch file first to use the Scheduler.
"C:\Program Files\R\R-3.0.2\bin\x64\Rscript.exe"
假设您的 R 脚本是
mytest.r
,位于D:\mydocuments\
中,您可以创建一个包含以下命令的批处理文件:然后将其添加为新任务,到Windows任务计划程序,在那里设置触发条件。
您也可以省略批处理文件。在任务计划程序的
program/script
文本框中设置C:\R\R-2.10.1\bin\Rcmd.exe
,并给出Arguments
code> 初始命令的其余部分:BATCH D:\mydocuments\mytest.r
通过 Windows 任务计划程序安排 R 任务(发布于 2015 年 2 月 11 日)
taskscheduleR:使用 Windows 调度 R 脚本的 R 包任务管理器(发布于2016年3月17日)
编辑
我最近再次采用了批处理文件的使用,因为我希望最小化cmd窗口(我找不到其他方法) 。
具体来说,我按如下方式填写 Windows 任务计划程序
Actions
选项卡:Program/script:
cmd.exe
Add Arguments (Optional):
/c start /min D:\mydocuments\mytest.bat ^&退出
mytest.bat的内容:
C:\R\R-3.5.2\bin\x64\Rscript.exe D:\mydocuments\mytest.r params
Supposing your R script is
mytest.r
, located inD:\mydocuments\
, you can create a batch file including the following command:Then add it, as a new task, to windows task scheduler, setting there the triggering conditions.
You could also omit the batch file. Set
C:\R\R-2.10.1\bin\Rcmd.exe
in theprogram/script
textbox in task scheduler, and give asArguments
the rest of the initial command:BATCH D:\mydocuments\mytest.r
Scheduling R Tasks via Windows Task Scheduler (Posted on February 11, 2015)
taskscheduleR: R package to schedule R scripts with the Windows task manager (Posted on March 17, 2016)
EDIT
I recently adopted the use of batch files again, because I wanted the cmd window to be minimized (I couldn't find another way).
Specifically, I fill the windows task scheduler
Actions
tab as follows:Program/script:
cmd.exe
Add arguments (optional):
/c start /min D:\mydocuments\mytest.bat ^& exit
Contents of mytest.bat:
C:\R\R-3.5.2\bin\x64\Rscript.exe D:\mydocuments\mytest.r params
现在 RStudio 中有内置选项可以执行此操作,首先运行调度程序安装下面的软件包
安装后转到
Now there is built in option in RStudio to do this, to run scheduler first install below packages
After installing go to
设置任务计划程序
步骤 1) 打开任务计划程序(开始 > 搜索任务计划程序)
步骤 2) 单击“操作”> “创建任务”
步骤3)选择“仅在用户登录时运行”,取消选中“以最高权限运行”,为您的任务命名,
配置“Windows Vista/Windows Server 2008”
步骤 4) 在“触发器”选项卡下,设置您希望脚本运行的时间
步骤 5) 在“操作”选项卡下,输入完整的脚本Rscript.exe 文件的位置,即将
脚本的名称与
-e
和source()
放在参数中,如下所示:对任务计划程序中计划的 Rscript 进行故障排除
当您使用任务计划程序运行脚本时,很难对任何问题进行故障排除,因为您不会收到任何错误消息。
这可以通过使用 R 中的
sink()
函数来解决,该函数允许您将所有错误消息输出到您指定的文件中。执行此操作的方法如下:要使 Rscript 正常工作,您必须更改的另一件事是指定脚本中任何文件路径的完整文件路径。
这在任务计划程序中不起作用:
您将需要指定在 Rscript 中获取的任何脚本的完整文件路径:
此外,我将从您在 R 脚本中引用的任何文件路径中删除任何特殊字符。例如:
可能无法运行。相反,请尝试:
更改 Windows 密码
注意:更改 Windows 密码将暂停您的任务计划程序脚本。您将需要重新登录任务计划程序并输入密码才能再次启动它们。
Setting up the task scheduler
Step 1) Open the task scheduler (Start > search Task Scheduler)
Step 2) Click "Action" > "Create Task"
Step 3) Select "Run only when the user is logged on", uncheck "Run with highest priveledges", name your task,
configure for "Windows Vista/Windows Server 2008"
Step 4) Under the "Triggers" tab, set when you would like the script to run
Step 5) Under the "Actions" tab, put the full location of the Rscript.exe file, i.e.
Put the name of your script with with
-e
andsource()
in arguments wrapping it like this:Troubleshooting a Rscript scheduled in the Task Scheduler
When you run a script using the Task Scheduler, it is difficult to troubleshoot any issues because you don't get any error messages.
This can be resolved by using the
sink()
function in R which will allow you to output all error messages to a file that you specify. Here is how you can do this:The other thing that you will have to change to make your Rscript work is to specify the full file path of any file paths in your script.
This will not work in task scheduler:
You will need to specify the full file path of any scripts you are sourcing within your Rscript:
Additionally, I would remove any special characters from any file paths that you are referencing in your R script. For example:
may not run. Instead, try:
Changing windows passwords
Beware: Changing windows passwords will pause your task scheduler script(s). You will need to log back into the task scheduler and enter your password to get them started again.
我通过
SCHTASKS
程序设置我的任务。要在启动时运行脚本,您可以按照以下内容编写一些内容,请参阅此网站了解更多详细信息
SCHTASKS
。更多详细信息,请访问 Microsoft 网站。I set up my tasks via the
SCHTASKS
program. For running scripts on startup, you would write something along the lines ofSee this website for more details on
SCHTASKS
. More details at Microsoft's website.您可以使用Windows 任务计划程序。
You can use Windows Task Scheduler.
执行这些步骤的任意组合后,如果您在 R.exe 运行后收到
“Argument Batch Ignored”
错误,请尝试此操作,它对我有用。在 Windows 任务计划程序中:
将参数字段中的
BATCH "C:\Users\desktop\yourscript.R"
替换为
CMD BATCH --vanilla --slave "C:\Users\desktop\你的脚本.R”
After following any combination of these steps and you receive the
"Argument Batch Ignored"
error after R.exe runs, try this, it worked for me.In Windows Task Scheduler:
Replace
BATCH "C:\Users\desktop\yourscript.R"
in the arguments fieldwith
CMD BATCH --vanilla --slave "C:\Users\desktop\yourscript.R"