windows 调度 R 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2793389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:18:43  来源:igfitidea点击:

Scheduling R Script

rwindowsscheduler-faq

提问by ETD

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.

我编写了一个 R 脚本,它从数据库中提取一些数据,对其执行多项操作并将输出发布到新数据库。

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.

任何人都可以推荐我可以查看的资源来解决这个问题吗?我在 Windows 机器上运行这个脚本。

回答by petermeissner

Actually under Windows you do not even have to create a batch file first to use the Scheduler.

实际上,在 Windows 下,您甚至不必先创建批处理文件即可使用调度程序

  • Open the scheduler: START -> All Programs -> Accesories -> System Tools -> Scheduler
  • Create a new Task
  • under tab Action, create a new action
  • choose Start Program
  • browse to Rscript.exewhich should be placed e.g. here:
    "C:\Program Files\R\R-3.0.2\bin\x64\Rscript.exe"
  • input the name of your file in the parametersfield
  • input the path where the script is to be found in the Start infield
  • go to the Triggerstab
  • create new trigger
  • choose that task should be done each day, month, ... repeated several times, or whatever you like
  • 打开调度程序:开始 -> 所有程序 -> 附件 -> 系统工具 -> 调度程序
  • 创建一个新任务
  • 在选项卡操作下,创建一个新操作
  • 选择启动程序
  • 浏览到Rscript.exe,它应该放在例如这里:
    “C:\Program Files\R\R-3.0.2\bin\x64\Rscript.exe”
  • 参数字段中输入文件名
  • Start in字段中输入要在其中找到脚本的路径
  • 转到触发器选项卡
  • 创建新触发器
  • 选择该任务应该每天,每月,...重复几次,或者任何你喜欢的

回答by George Dontas

Supposing your R script is mytest.r, located in D:\mydocuments\, you can create a batch file including the following command:

假设您的 R 脚本mytest.r位于D:\mydocuments\,您可以创建一个包含以下命令的批处理文件:

C:\R\R-2.10.1\bin\Rcmd.exe BATCH D:\mydocuments\mytest.r

Then add it, as a new task, to windows task scheduler, setting there the triggering conditions.

然后将它作为一个新任务添加到windows任务调度程序中,在那里设置触发条件。

You could also omit the batch file. Set C:\R\R-2.10.1\bin\Rcmd.exein the program/scripttextbox in task scheduler, and give as Argumentsthe rest of the initial command: BATCH D:\mydocuments\mytest.r

您也可以省略批处理文件。在任务调度程序C:\R\R-2.10.1\bin\Rcmd.exeprogram/script文本框中设置,并作为Arguments初始命令的其余部分给出:BATCH D:\mydocuments\mytest.r

Scheduling R Tasks via Windows Task Scheduler(Posted on February 11, 2015)

通过 Windows Task Scheduler 调度 R 任务(2015 年 2 月 11 日发布)

taskscheduleR: R package to schedule R scripts with the Windows task manager(Posted on March 17, 2016)

taskscheduleR:使用 Windows 任务管理器调度 R 脚本的 R 包(发布于 2016 年 3 月 17 日)

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).

我最近又采用了批处理文件的使用方式,因为我想让cmd窗口最小化(我找不到其他方法)。

Specifically, I fill the windows task scheduler Actionstab as follows:

具体来说,我填写windows task scheduler Actionstab如下:

Program/script:

Program/script:

cmd.exe

命令行工具

Add arguments (optional):

Add arguments (optional):

/c start /min D:\mydocuments\mytest.bat ^& exit

/c start /min D:\mydocuments\mytest.bat ^& 退出

Contents of mytest.bat:

mytest.bat 的内容

C:\R\R-3.5.2\bin\x64\Rscript.exe D:\mydocuments\mytest.r params

C:\R\R-3.5.2\bin\x64\Rscript.exe D:\mydocuments\mytest.r 参数

回答by Zeeshan

Now there is built in option in RStudio to do this, to run scheduler first install below packages

现在在 RStudio 中有内置选项来执行此操作,首先运行调度程序安装在包下面

  install.packages('data.table')
  install.packages('knitr')
  install.packages('miniUI')
  install.packages('shiny')
  install.packages("taskscheduleR", repos = "http://www.datatailor.be/rcube", type = 
  "source")

After installing go to

安装后去

**TOOLS -> ADDINS ->BROWSE ADDINS ->taskscheduleR -> Select it and execute it.**

enter image description here

在此处输入图片说明

回答by Roman Lu?trik

I set up my tasks via the SCHTASKSprogram. For running scripts on startup, you would write something along the lines of

我通过SCHTASKS程序设置了我的任务。为了在启动时运行脚本,你会写一些类似的东西

SCHTASKS /Create /SC ONSTART /TN MyProgram /TR "R CMD BATCH --vanilla d:\path\to\script.R"

See this websitefor more details on SCHTASKS. More details at Microsoft's website.

有关更多详细信息,请参阅此网站SCHTASKS。更多详细信息,请访问Microsoft 网站

回答by Mark Byers

回答by ben.watson.ca

Setting up the task scheduler

设置任务调度程序

Step 1) Open the task scheduler (Start > search Task Scheduler)

步骤1)打开任务计划程序(开始>搜索任务计划程序)

Step 2) Click "Action" > "Create Task"

步骤 2) 点击“操作”>“创建任务”

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"

步骤 3) 选择“仅在用户登录时运行”,取消选中“以最高权限运行”,命名您的任务,配置为“Windows Vista/Windows Server 2008”

enter image description here

在此处输入图片说明

Step 4) Under the "Triggers" tab, set when you would like the script to run

步骤 4) 在“触发器”选项卡下,设置您希望脚本何时运行

Step 5) Under the "Actions" tab, put the full location of the Rscript.exe file, i.e.

步骤5)在“Actions”选项卡下,放入Rscript.exe文件的完整位置,即

"C:\Program Files\R\R-3.6.2\bin\Rscript.exe" (include the quotes)

Put the name of your script with with -eand source()in arguments wrapping it like this:

把你的脚本的名字用 with-esource()in 参数包裹起来,如下所示:

-e "source('C:/location_of_my_script/test.R')"

enter image description here

在此处输入图片说明

Troubleshooting a Rscript scheduled in the Task Scheduler

对任务计划程序中计划的 Rscript 进行故障排除

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:

这可以通过使用sink()R 中的函数来解决,该函数允许您将所有错误消息输出到您指定的文件。以下是您可以这样做的方法:

# Set up error log ------------------------------------------------------------
error_log <- file("C:/location_of_my_script/error_log.Rout", open="wt")
sink(error_log, type="message")

try({

# insert your code here

})

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.

您必须更改以使 Rscript 工作的另一件事是指定脚本中任何文件路径的完整文件路径。

This will not work in task scheduler:

这在任务调度程序中不起作用:

source("./functions/import_function.R")

You will need to specify the full file path of any scripts you are sourcing within your Rscript:

您需要指定您在 Rscript 中采购的任何脚本的完整文件路径:

source("C:/location_of_my_script/functions/import_function.R")

回答by Starbucks

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.

在执行这些步骤的任意组合后,您"Argument Batch Ignored"在 R.exe 运行后收到错误,试试这个,它对我有用。

In Windows Task Scheduler:

在 Windows 任务计划程序中:

Replace BATCH "C:\Users\desktop\yourscript.R"in the arguments field

BATCH "C:\Users\desktop\yourscript.R"在参数字段中替换

with

CMD BATCH --vanilla --slave "C:\Users\desktop\yourscript.R"

CMD BATCH --vanilla --slave "C:\Users\desktop\yourscript.R"