pet*_*ner 99
实际上在Windows下,您甚至不必首先创建批处理文件来使用调度程序.
Geo*_*tas 57
假设您的R脚本mytest.r
位于D:\mydocuments\
,您可以创建包含以下命令的批处理文件:
C:\R\R-2.10.1\bin\Rcmd.exe BATCH D:\mydocuments\mytest.r
Run Code Online (Sandbox Code Playgroud)
然后将其作为新任务添加到Windows任务计划程序,在那里设置触发条件.
您也可以省略批处理文件.在任务调度程序C:\R\R-2.10.1\bin\Rcmd.exe
的program/script
文本框中设置,并作为Arguments
初始命令的其余部分:BATCH D:\mydocuments\mytest.r
通过Windows任务计划程序安排R任务(发布于2015年2月11日)
taskscheduleR:用于使用Windows任务管理器调度R脚本的R包(发布于2016年3月17日)
Zee*_*han 11
现在在 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")
Run Code Online (Sandbox Code Playgroud)
安装后去
**TOOLS -> ADDINS ->BROWSE ADDINS ->taskscheduleR -> Select it and execute it.**
Run Code Online (Sandbox Code Playgroud)
设置任务调度程序
步骤1)打开任务计划程序(开始>搜索任务计划程序)
步骤 2) 点击“操作”>“创建任务”
步骤 3) 选择“仅在用户登录时运行”,取消选中“以最高权限运行”,命名您的任务,配置为“Windows Vista/Windows Server 2008”
步骤 4) 在“触发器”选项卡下,设置您希望脚本何时运行
步骤5)在“Actions”选项卡下,放入Rscript.exe文件的完整位置,即
"C:\Program Files\R\R-3.6.2\bin\Rscript.exe" (include the quotes)
Run Code Online (Sandbox Code Playgroud)
把你的脚本的名字用 with-e
和source()
in 参数包裹起来,如下所示:
-e "source('C:/location_of_my_script/test.R')"
Run Code Online (Sandbox Code Playgroud)
对任务计划程序中计划的 Rscript 进行故障排除
当您使用任务计划程序运行脚本时,很难解决任何问题,因为您不会收到任何错误消息。
这可以通过使用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
})
Run Code Online (Sandbox Code Playgroud)
您必须更改以使 Rscript 工作的另一件事是指定脚本中任何文件路径的完整文件路径。
这在任务调度程序中不起作用:
source("./functions/import_function.R")
Run Code Online (Sandbox Code Playgroud)
您需要指定您在 Rscript 中采购的任何脚本的完整文件路径:
source("C:/location_of_my_script/functions/import_function.R")
Run Code Online (Sandbox Code Playgroud)
此外,我会从您在 R 脚本中引用的任何文件路径中删除任何特殊字符。例如:
df <- fread("C:/location_of_my_data/file#2342.csv")
Run Code Online (Sandbox Code Playgroud)
可能无法运行。相反,请尝试:
df <- fread("C:/location_of_my_data/file_2342.csv")
Run Code Online (Sandbox Code Playgroud)