2017-06-28 66 views
0

我有一個'R'文件,它插入從數據庫中讀取數據,執行一些計算,然後將數據重新插入表中。R腳本.bat文件 - 執行前添加源文件

之前,我執行腳本,我跑「源」,如下..

R Source

我想使用Windows任務計劃程序自動調度此腳本運行。我下面的指導https://trinkerrstuff.wordpress.com/2015/02/11/scheduling-r-tasks-via-windows-task-scheduler/ - 當創建.bat文件就應該是這個樣子:

echo off 
CMD BATCH C:\PATHNAME\RSCRIPT.R 

,我應該插入什麼位置,以確保它運行的「來源」第一?

在R代碼,我

在代碼中我有:

#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") 

但我需要運行我的整個代碼之前單擊源按鈕,或者我得到一個錯誤(如下) 。

> #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 
+0

您可以將'source()'命令添加到您的腳本以獲取文件。不確定這些文件中究竟是什麼,很難準確地告訴你在做什麼。 – MrFlick

+0

我已經添加了一些代碼可能有幫助 – SwiftBeginner

回答

1

鑑於您的腳本,應該沒有必要先單擊「源」按鈕:實際上,它會執行您的腳本兩次。

有幾件事情對你的腳本:

CMD BATCH C:\PATHNAME\RSCRIPT.R 

這是在CMD BATCH前失蹤R。但是,您應該使用Rscript.exe而不是R CMD BATCH。這是一個現代化的替代品。

source("..//R-Utilities//Utilities.R") 

沒有必要爲雙斜線:單斜槓工作。

source("../R-Utilities/Utilities.R") 

更重要的,以這種方式使用source可以快速由於幾個缺點(例如圓形夾雜,相對路徑等)變得複雜且容易出錯的。實現這一目標的更好方法是通過<modules>軟件包,該軟件包可極大地改進source函數的替代品,這似乎非常適合您的使用情況。

特別是,你的腳本可能會無法正常工作:source將無法​​找到文件R-Utilities/Utilities.RCTB_functions.R,因爲它搜索這些相對於當前的工作目錄,而不是相對於腳本目錄。使用 修復了這個問題。

+0

我添加了錯誤日誌,當我運行我的代碼時無需使用源代碼按鈕 – SwiftBeginner

+0

@SwiftBeginner導致錯誤的代碼是*完全不同的代碼*顯示! –