2017-06-28 17 views
0

データベースから読み込みデータを挿入し、計算を行った後、データをテーブルに再挿入します。Rスクリプト.batファイル - 実行前にソースを追加する

私はスクリプトを実行する前に、私は実行するために、自動スケジュールにこのスクリプトをWindowsのタスクスケジューラを使用したい

R Source

..「ソース」と以下を実行します。私はガイド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

スクリプトを指定すると、最初に「ソース」ボタンをクリックする必要はありません。実際にはスクリプトを2回実行します。

スクリプトに関するいくつかのこと:

CMD BATCH C:\PATHNAME\RSCRIPT.R 

これはCMD BATCHの前でRが不足しています。ただし、おそらくR CMD BATCHの代わりにRscript.exeを使用してください。それは現代の交換品です。

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

ダブルスラッシュのための必要はありません:単一のスラッシュが働きます。

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

もっと基本的に、このようにsourceを使用して迅速に起因するいくつかの欠点(例えば円形の包含、相対パスなど)に複雑でエラーが発生しやすいとなることができます。これを実現するより良い方法は、<modules>パッケージを使用することです。これは、source関数の置換えを大幅に改善しました。これは、使用例に非常によく似ています。特に

、スクリプトはおそらく動作しません:それはスクリプトディレクトリに対する、現在の作業ディレクトリにこれらの相対ません検索するためsourceファイルR-Utilities/Utilities.RCTB_functions.Rを見つけることができません。 を使用するとこれが修正されます。

+0

ソースコードを使わずにコードを実行するとエラーログが表示される – SwiftBeginner

+0

@SwiftBeginnerエラーの原因となるコードは、表示されます! –

関連する問題