minhphongb
minhphongb

Reputation: 111

R Batch mode - Can't execute script

To run R non-interactively I've decided to use Batch processing (using Windows 7). What I've entered the command prompt (CMD)

cd c:\Program Files\R\R-3.1.3\bin\x64 #set directory
"c:\Program Files\R\R-3.1.3\bin\x64\R.exe" CMD BATCH "c:\Program Files\R\R-3.1.3\bin\myscript.R" "c:\Program Files\R\R-3.1.3\bin\output.out"

The output was generated. I've opened the output.out file and the following error message is visible:

> myData1<-read.csv("myData.csv",header=T, sep=";", dec=",")
> temp<-read.csv("Temperature.csv",header=T, sep=";", dec=",")
Error in file(file, "rt") : cannot open the connection
Calls: read.csv -> read.table -> file
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'Temperature.csv': No such file or directory
Execution halted

Within the myscript.R file, I've already set the working directory to where the files (myData.csv and Temperature.csv) are located. When I execute the content of myscript.R in Rstudio, it all works. Somehow this error keeps popping up and I am totally clueless. Any suggestions?

Upvotes: 1

Views: 1901

Answers (2)

rmuc8
rmuc8

Reputation: 2989

Quoting Michael Lundholm's pdf Working with R in batch mode: Some notes for beginners

Although R is installed, the Windows system does not find the file R.exe. This is because this file is in a directory which is not in the default search path of Windows. The reason is that the default behaviour of the installation script is to install new version of R in different directories. It is up to the user to decide where Windows should look for R (i.e., which version to use).

If this error message occurs we have to change the search path so that it includes the path to the directory where R.exe is. We do this as follows:

  1. Open the Control Panel, choose ‘System’, click on ‘Advanced system settings’ in the menue to the left and choose the ‘Environment variables’ button; see Figure 5.
  2. Choose the variable ‘Path’ and click on the Edit–button. Now we have to edit Variable value’ so that it also contains the path to the directory where the various R binaries (programs) are installed.
  3. The path to this file is found in Windows explorer by looking for the directories were R was installed. See Figure 6. Typically the path is something like C:\Program Files\R\R-2.15.1\bin\x64 and it should be appended to the already existing path in ‘Variable value’.
  4. Note that the various paths in the search path as separated with a semi–colon so we should add an initial ‘;’ to the string characters that we append to ‘Variable value’. The result can be seen in Figure 7.

Once you've added the path to the environment variables, you call Rscript from the directory your csv file is stored in

Rscript myscript.R arg1 arg2 arg3

or you change the path of

 temp <- read.csv("Temperature.csv", header=T, sep=";", dec=",")

in the R file to

 temp <- read.csv("C:/Users/.../Temperature.csv", header=T, sep=";", dec=",")

Upvotes: 1

jangorecki
jangorecki

Reputation: 16697

In your example you are trying to look for csv file in the cd c:\Program Files\R\R-3.1.3\bin\x64.

You need to execute R.exe or even better Rscript.exe in the directory where myData.csv is located.

R exe dir is one thing.
R script dir is another. csv dir is another.

Use absolute paths or...
Considering you have your R script in the same directory as csv file you can use kind of:

CD /path_to_R_script/
path_to_exe/exe script

So you run the exe from the directory where you have csv file.

In linux it could look like:

cd /my/path && Rscript myscript.R

Upvotes: 2

Related Questions