[英]R Script .bat file - Adding Source before executing
I have an 'R' file which inserts reads data from a database, does some calculations, then re-inserts data back into a table.我有一个“R”文件,它从数据库中插入读取数据,进行一些计算,然后将数据重新插入表中。
Before i execute the script, I run 'Source' as below..在我执行脚本之前,我运行“源”如下..
I want to use Windows Task Scheduler to auto schedule this script to run.我想使用 Windows 任务计划程序自动安排此脚本运行。 I am following the guide https://trinkerrstuff.wordpress.com/2015/02/11/scheduling-r-tasks-via-windows-task-scheduler/ - When creating the .BAT file it should look something like:
我正在遵循指南https://trinkerrstuff.wordpress.com/2015/02/11/scheduling-r-tasks-via-windows-task-scheduler/ - 创建 .BAT 文件时,它应该类似于:
echo off
CMD BATCH C:\PATHNAME\RSCRIPT.R
What should i insert here to make sure it runs the 'Source' first?我应该在这里插入什么以确保它首先运行“源”?
In the R code i have在 R 代码中,我有
In the code i have:在代码中我有:
#use a relative path to locate our common utilities file and source it
source("..//R-Utilities//Utilities.R")
# use check_install_package function from Utilities.R to install and load
packages
check_install_package("lubridate")
check_install_package("plyr")
check_install_package("dplyr")
check_install_package("dtplyr")
check_install_package("ISOweek")
check_install_package("stringi")
check_install_package("RODBC")
#give us access to the library of functions this script uses
source("CTB_functions.R")
But I need to click the source button before running my whole code, or i get an error (As below).但是我需要在运行我的整个代码之前单击源按钮,否则我会收到错误消息(如下所示)。
> #this automatically sets the working directory to be where this file is
> setwd(getSrcDirectory(function(x) {x}))
Error in setwd(getSrcDirectory(function(x) { :
cannot change working directory
>
> #use a relative path to locate our common utilities file and source it
> source("../R-Utilities/Utilities.R")
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file '../R-Utilities/Utilities.R': No such file or directory
>
> # use check_install_package function from Utilities.R to install and load
packages
> check_install_package("lubridate")
Error: could not find function "check_install_package"
> check_install_package("plyr")
Error: could not find function "check_install_package"
> check_install_package("dplyr")
Error: could not find function "check_install_package"
> check_install_package("dtplyr")
Error: could not find function "check_install_package"
> check_install_package("ISOweek")
Error: could not find function "check_install_package"
> check_install_package("stringi")
Error: could not find function "check_install_package"
> check_install_package("RODBC")
Error: could not find function "check_install_package"
>
> #give us access to the library of functions this script uses
> source("CTB_functions.R")
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file 'CTB_functions.R': No such file or directory
Given your script, there should be no need to click the “Source” button first: in fact, it would execute your script twice.给定您的脚本,不需要先单击“源”按钮:实际上,它会执行您的脚本两次。
A few things about your scripts:关于你的脚本的一些事情:
CMD BATCH C:\\PATHNAME\\RSCRIPT.R
This is missing R
in front of CMD BATCH
.这是
CMD BATCH
前面缺少的R
。 However, You should probably use Rscript.exe
instead of R CMD BATCH
.但是,您可能应该使用
Rscript.exe
而不是R CMD BATCH
。 It's a modern replacement.这是一个现代的替代品。
source("..//R-Utilities//Utilities.R")
There's no need for double slashes: single slashes work.不需要双斜线:单斜线工作。
source("../R-Utilities/Utilities.R")
More fundamentally, using source
in this manner can quickly become complex and error-prone due to several shortcomings (eg circular inclusion, relative paths etc).更根本的是,由于一些缺点(例如圆形包含、相对路径等),以这种方式使用
source
会很快变得复杂和容易出错。 A better way of achieving this is via the ' box ' package, which provides a vastly improved replacement for the source
function, which seems to fit your use-case very closely.实现这一点的更好方法是通过 ' box ' 包,它为
source
函数提供了一个大大改进的替代品,它似乎非常适合您的用例。
In particular, your script will probably not work: source
won't find the files R-Utilities/Utilities.R
and CTB_functions.R
because it searches these relative to the current working directory, not relative to the script directory .特别是,您的脚本可能无法运行:
source
不会找到文件R-Utilities/Utilities.R
和CTB_functions.R
因为它搜索这些文件是相对于当前工作目录的,而不是相对于脚本目录的。 Using 'box' fixes this.使用“盒子”可以解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.