R script from command line

2020-05-28 02:11发布

I wanted to run this example script: http://mazamascience.com/WorkingWithData/?p=912 from Windows command line. So I opened the command line and typed Rscript tryCatch.R 1. However, I keep getting the error message Error: R not found. I did set the PATH environment variable as C:\Programme\R\R-3.0.1\bin. If I just type R.exe, it does start R, but it cannot find the packages that are to be loaded at start (e.g. package 'utils' in options<"defaultPackages"> was not found). I guess I have to set another path to the libraries somewhere, but I haven't got any idea where to do this.

UPDATE: After explicitly typing PATH C:\Programme\R\R-3.0.1\bin (rather than just adding this to the value of the environment variable PATH) it seems that R is found. However, a new problem occurs: In normalizePath<path.expand(path), winslash, mustWork>: path[2] = "C:/Programme/R/R-3.0.1/library": Access denied, the same than for the methods library. Then: Calls: .First ... library -> .getRequiredPackages2 -> library -> normalizePath Execution stopped. I'm using Windows 7 and I do have administrator rights.

3条回答
聊天终结者
2楼-- · 2020-05-28 02:32

Rscript is very handy (R CMD BATCH is the old way to ) specially under windows, But generally under I create a batch file to avoid all path's headache.

For example say launcher.bat:

@echo off
C:
PATH C:\Programme\R\R-3.0.1\bin;%path%
cd PATH_TO_YOUR_RSCRIPT
Rscript tryCatch.R 1
pause

And open a console(using cmd) , go where you have stored your launcher.bat and launch it. Or from the R cosnole using shell:

shell('path_to_launcher\launcher.bat')
查看更多
欢心
3楼-- · 2020-05-28 02:32

I've found out that it was a language-specific problem on Windows 7, similar to what is described here: https://stat.ethz.ch/pipermail/r-help/2011-May/276932.html

After changing PATH to C:\Program Files\R\R-3.0.1\bin the script is properly executed from the command prompt.

Thanks to everyone who tried to help!

查看更多
时光不老,我们不散
4楼-- · 2020-05-28 02:50

I ran into this problem under windows 7, apparently, when setting environment variables>user variables the path is not added into the PATH, so the user must add this path in system variables > PATH at the end just add the path to your .EXE files and voila.

查看更多
登录 后发表回答