When trying to run R script from CMD, getting "Fatal error: cannot open file 'CMD': No such file or directory" - r

I'm trying to create a batch file to run an R script for me automatically on Windows 7.
Batch file:
"C:\Program Files\R\R-3.2.3\bin\Rscript.exe" CMD BATCH "C:\Users\<my username>\Documents\R\Script Testing\script.R"
pause
R script:
write.csv("hello", "automatic_output.txt")
This script works when I run it in R, but when I try to run the .bat, I get this error:
Fatal error: cannot open file 'CMD': No such file or directory
This happens regardless of whether I paste the code into the command prompt, run the .bat as administrator, or schedule the task.
I tried following the advice given in this thread:
Exporting .csv from R & Batch file
but am seeing the same behavior.
I also used the Windows search to try to find "automatic_output.txt" in case it was being stored in an unexpected location but no results came up.
Lastly,
https://stackoverflow.com/a/23514987 seemed to suggest that I could omit the "CMD BATCH", and when I do I no longer get an error, but automatic_output.txt is still not produced.
Any ideas?

As Thomas pointed out, I needed to change "Rscript.exe" to "R.exe". For future googlers, here is the line of code that worked:
"C:\Program Files\R\R-3.2.3\bin\R.exe" CMD BATCH "C:\Users\<my username>\Documents\R\Script Testing\script.R"

Related

Error that says Rscript is not recognized as an internal or external command, operable program or batch file [duplicate]

shell_exec("Rscript C:\R\R-3.2.2\bin\code.R ");
This is the call to script.On calling the above script, the error occurs.
I am trying to call my R script from the above path but no output is being shown. While checking the error logs of PHP, it says 'Rscript' is not recognized as an internal or external command, operable program or batch file.' The script is working fine on the Rstudio but not running on the command line.
Add the Rscript path to your environment variables in Windows:
Go to Control Panel\System and Security\System and click Advanced System Settings, then environment variables, click on path in the lower box, edit, add "C:\R\R-3.2.2\bin"
Restart everything. Should be good to go. Then you should be able to do
exec('Rscript PATH/TO/my_code.R')
instead of typing the full path to Rscript. Won't need the path to your my_code.R script if your php file is in the same directory.
You need to set the proper path where your RScript.exe program is located.
exec ("\"C:\\R\\R-3.2.2\\bin\\Rscript.exe\"
C:\\My_work\\R_scripts\\my_code.R my_args";
#my_args only needed if you script take `args`as input to run
other way is you declare header in your r script (my_code.r)
#!/usr/bin/Rscript
and call it from command line
./my_code.r
If you are running it in Git Bash terminal, you could follow a revised version of the idea suggested by #user5249203: in the first line of your file my_code.R, type the following
#!/c/R/R-3.2.2/bin/Rscript.exe
I assumed that your path to Rscript.exe is the one listed above C:\R\R-3.2.2\bin. For anyone having a different path to Rscript.exe in Windows, just modify the path-to-Rscript accordingly. After this modification of your R code, you could run it in the Git Bash terminal using path-to-the-code/mycode.R. I have tested it on my pc.
I faced the same problem while using r the first time in VS Code, just after installing the language package (CRAN).
I restart the application and everything worked perfectly. I think restarting would work for you as well.

Running R script using command line on Windows

I'm trying for my first time to run an R script from command line on WINDOWS to automate calculation with another program. So I made my R script using R studio and saved it a .bat file with this line
C:\Program Files\R\R-4.0.2\bin CMD BATCH C:\my_directory\my_script.R
Then I putted the script and files the script have to be executed on in the same directory, the one that contains files to run also the program I need. I came throught command line in that directory and I executed the batch file but it doesn't work. I have this as a error message:
"C:\Program Files\R\R-4.0.2\bin"it is not recognized as an internal or external command,
an executable program or batch file.
Where I am wrong? thank you!
You should use Rscript instead, assuming its in your PATH.
E.g.
Rscript C:\my_directory\my_script.R
Or a single expression
Rscript -e "print(123)"

Error from CMD running R statement and R file

I am trying to run R script and statement in win10 CMD with the following lines:
D:\Program Files\R\R-3.4.2\bin>r CMD BATCH E:\R scripts\AlgoTraining0101.R
D:\Program Files\R\R-3.4.2\bin>Rscript -e "library(C50)"
E:\R scripts\AlgoTraining0101.R is a R script file I created.
I get an error:
'D:\Program' this is not an internal or external command, or batch
file...
When I run this
D:\Program Files\R\R-3.4.2\bin>Rscript -e "C50Training<-read.csv(file="E:/algo/dataset/C50Training.csv",header=TRUE)"
it says 'The system cannot find the file'.
The R command lines obviously had no problem in R studio but it does not work out in CMD. Can anyone help? I have searched but havent found anyone encourtering the same issue.
Thanks
It is a best practice to quote paths and file names. This should be done whether they need it or not today. Someday they might. It also prevents frustration if it is a habit.
The error message is complaining that C:\Program cannot be found.
"D:\Program Files\R\R-3.4.2\bin\r" CMD BATCH "E:\R scripts\AlgoTraining0101.R"
"D:\Program Files\R\R-3.4.2\bin>Rscript" -e "library(C50)"
Try running it with quotation marks, e.g.
"R" CMD BATCH "path/to/script.R"
# if R is has not been added to PATH
"path/to/R/R-x.y.z/bin" CMD BATCH "path/to/script.R"
I usually save the above as .bat file and am good to go.
I tried this with quote
D:\Program Files\R\R-3.4.2\bin\x64>R CMD BATCH "E:\R scripts\AlgoTraining0101.R"
It still gave me the error message 'E:\R' is not recognized as anything that the system understands. Then I changed the folder name from 'R scripts' to 'R_scripts' and ran this.
D:\Program Files\R\R-3.4.2\bin\x64>R CMD BATCH "E:\R_scripts\AlgoTraining0101.R"
It went through...So the quotation marks did not help here.

Rscript.exe cannot open/read scripts, permission denied

So I'm trying to run some R scripts via cmd directly using
Rscript.exe "C:\Users\xxxxx\Documents\test.R"
but it fails every time with the same error :
Fatal error: cannot open file 'C:\Users\xxxxx\Documents\test.R': Permission denied
However, if I run it via
R CMD BATCH "C:\Users\xxxxx\Documents\test.R"
Then it works. Do you know why Rscript cannot reach the file ? I would really like to use this rather the R CMD BATCH approach.
Everything is in the PATH and I tried moving the .R script file around to no avail.
Thanks!

Problems executing script from command line in R. Error message: cannot find path specified

I have been trying to execute a simple test.R
setwd("C:\Users\jdd\Documents")
test <- 2*6598
filename = "test.csv"
write.csv(test,file=filename)
Via the following command line command in Windows:
"C:\Program Files\R\R-2.15.2\bin\R.exe" CMD BATCH --vanilla --slave "C:\Users\jdd\Documents\test.R"
When I execute this I get the following error:
The system cannot find the path specified.
I have been trying to work out a solution on the basis of the provided error message, but failed so far. Wondering if somebody can help me so I can execute the script directly from the command line. Thanks
Thanks #sebastian-c! I tried to use RScript, which I investigated before. However, the problem was a different one. Appears that in my installation there is a R.exe and Rscript.exe file in .\bin, but also one in .\bin\x64. The first one is not working properly, but the second one is. The comment made by #Roland is very important as well, since once working I got this error message!
The following command did the job:
"C:\Program Files\R\R-2.15.2\bin\x64\Rscript.exe" "C:\Users\jdd\Documents\test.R"
and the corrected text.R is:
setwd("C:\\Users\\jdd\\Documents")
test <- 2*6598
filename = "test.csv"
write.csv(test,file=filename)
As mentioned here, it might has something to do with 64bit version of R.
The problem is that Rscript.exe itself is attempting to access a missing file on the system. The obvious fix is explicitly add 'x64' to the path of the other Rscript.exe that was installed:
"C:\Program Files\R\R-2.15.2\bin\x64\Rscript.exe" --version
R scripting front-end version 3.0.2 (2013-09-25)

Resources