调度 R 脚本

发布于 2024-08-31 09:54:10 字数 148 浏览 6 评论 0原文

我编写了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

爱你不解释 2024-09-07 09:54:10

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

  • 打开调度程序:开始->所有程序 ->配件->系统工具->计划程序
  • 创建一个新任务
  • 在“操作”选项卡下
  • ,创建一个新操作选择启动程序
  • 浏览到Rscript.exe应放置在例如此处:

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

  • 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.exe which 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 parameters field
  • input the path where the script is to be found in the Start in field
  • go to the Triggers tab
  • create new trigger
  • choose that task should be done each day, month, ... repeated several times, or whatever you like
夜深人未静 2024-09-07 09:54:10

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

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

然后将其添加为新任务,到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 in D:\mydocuments\, you can create a batch file including the following command:

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.

You could also omit the batch file. Set C:\R\R-2.10.1\bin\Rcmd.exe in the program/script textbox in task scheduler, and give as Arguments 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

鹿童谣 2024-09-07 09:54:10

现在 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")

安装后转到

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

在此处输入图像描述

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

  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

聽兲甴掵 2024-09-07 09:54:10

设置任务计划程序

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

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

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

在此处输入图像描述

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

步骤 5) 在“操作”选项卡下,输入完整的脚本Rscript.exe 文件的位置,即将

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

脚本的名称与 -esource() 放在参数中,如下所示:

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

在此处输入图像描述

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

当您使用任务计划程序运行脚本时,很难对任何问题进行故障排除,因为您不会收到任何错误消息。

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

# 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

})

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

这在任务计划程序中不起作用:

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

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

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

此外,我将从您在 R 脚本中引用的任何文件路径中删除任何特殊字符。例如:

df <- fread("C:/location_of_my_data/file#2342.csv")

可能无法运行。相反,请尝试:

df <- fread("C:/location_of_my_data/file_2342.csv")

更改 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"

enter image description here

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.

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

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

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

enter image description here

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:

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

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:

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

Additionally, I would remove any special characters from any file paths that you are referencing in your R script. For example:

df <- fread("C:/location_of_my_data/file#2342.csv")

may not run. Instead, try:

df <- fread("C:/location_of_my_data/file_2342.csv")

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.

在巴黎塔顶看东京樱花 2024-09-07 09:54:10

我通过 SCHTASKS 程序设置我的任务。要在启动时运行脚本,您可以按照以下内容编写一些内容,

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

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

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

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

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

爱你是孤单的心事 2024-09-07 09:54:10

执行这些步骤的任意组合后,如果您在 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 field

with

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文