R批处理模式 - 无法执行脚本
R Batch mode - Can't execute script
非交互地 运行 R 我决定使用批处理(使用 Windows 7)。
我输入的命令提示符(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"
输出已生成。我打开了 output.out 文件,可以看到以下错误消息:
> 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
在 myscript.R 文件中,我已经将工作目录设置为文件(myData.csv 和 Temperature.csv)所在的位置。当我在Rstudio中执行myscript.R的内容时,一切正常。不知何故,这个错误不断出现,我完全无能为力。有什么建议吗?
在您的示例中,您试图在 cd c:\Program Files\R\R-3.1.3\bin\x64
.
中查找 csv 文件
你需要在myData.csv
所在的目录执行R.exe
或者更好的Rscript.exe
R exe 目录是一回事。
R 脚本目录是另一个。
csv 目录是另一个。
使用绝对路径或...
考虑到您的 R 脚本与 csv 文件位于同一目录中,您可以使用:
CD /path_to_R_script/
path_to_exe/exe script
所以你 运行 从你有 csv 文件的目录中的 exe。
在 linux 中它可能看起来像:
cd /my/path && Rscript myscript.R
引用 Michael Lundholm 的 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:
- 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.
- 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.
- 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’.
- 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.
将路径添加到环境变量后,从存储 csv 文件的目录调用 Rscript
Rscript myscript.R arg1 arg2 arg3
或者您更改
的路径
temp <- read.csv("Temperature.csv", header=T, sep=";", dec=",")
在R
文件中到
temp <- read.csv("C:/Users/.../Temperature.csv", header=T, sep=";", dec=",")
非交互地 运行 R 我决定使用批处理(使用 Windows 7)。 我输入的命令提示符(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"
输出已生成。我打开了 output.out 文件,可以看到以下错误消息:
> 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
在 myscript.R 文件中,我已经将工作目录设置为文件(myData.csv 和 Temperature.csv)所在的位置。当我在Rstudio中执行myscript.R的内容时,一切正常。不知何故,这个错误不断出现,我完全无能为力。有什么建议吗?
在您的示例中,您试图在 cd c:\Program Files\R\R-3.1.3\bin\x64
.
你需要在myData.csv
所在的目录执行R.exe
或者更好的Rscript.exe
R exe 目录是一回事。
R 脚本目录是另一个。
csv 目录是另一个。
使用绝对路径或...
考虑到您的 R 脚本与 csv 文件位于同一目录中,您可以使用:
CD /path_to_R_script/
path_to_exe/exe script
所以你 运行 从你有 csv 文件的目录中的 exe。
在 linux 中它可能看起来像:
cd /my/path && Rscript myscript.R
引用 Michael Lundholm 的 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:
- 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.
- 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.
- 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’.- 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.
将路径添加到环境变量后,从存储 csv 文件的目录调用 Rscript
Rscript myscript.R arg1 arg2 arg3
或者您更改
的路径 temp <- read.csv("Temperature.csv", header=T, sep=";", dec=",")
在R
文件中到
temp <- read.csv("C:/Users/.../Temperature.csv", header=T, sep=";", dec=",")