1

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!

7
  • 1
    I have not installed R, but the help output on running cmd /? in a command prompt window explains in last paragraph on last help page that a file name (or any other argument string) containing a space or one of these characters &()[]{}^=;!'+,`~ must be enclosed in " to get all the characters inside the double quoted argument string interpreted literally. So I suppose, the right command line is: "C:\Program Files\R\R-4.0.2\bin\R.exe" CMD BATCH "C:\my_directory\my_script.R" Commented Feb 14, 2021 at 16:46
  • 3
    To be more specific, you provided a directory as an internal or external command, instead of the executable file which lies within it, R.exe. The correct command is posted in the first comment above. Commented Feb 14, 2021 at 16:55
  • @Nancy now that you are actually using r.exe in your script, you should be getting a different error than before. We cant see your screen. You have to provide the error messages and debugging information. Saying it does not work does not help us help you. Commented Feb 14, 2021 at 20:57
  • 1
    Also the initial error message you posted does not match the code you posted. Based on your code the error would have been: 'C:\Program' is not recognized as an internal or external command, operable program or batch file. Commented Feb 14, 2021 at 21:04
  • with double quote I have ""C:\Program' is not recognized as an internal or external command, operable program or batch file"". Then I tried both with R.exe and Rscript.exe (after copying them in the Directory I'm interested in) and I have this error "Cannot find the specified path" Commented Feb 15, 2021 at 10:33

1 Answer 1

3

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)"
Sign up to request clarification or add additional context in comments.

5 Comments

trying as you suggested I have "Cannot find the specified path." also typing only "Rscript" so I suppose that the problem is with R
Then Rscript is not in your PATH, so you need to add it.
I added Rscript in the path, still the same. On linux when there is a file, it has to be executable. Is there something similar in windows? I'm not so experienced on windows
After you add Rscript to the path, in my case by adding the folder C:\Program Files\R\R-4.0.2\bin\x64 to the path, be sure to open and close CMD for PATH to refresh. Then open a new window of CMD, and type Rscript, and tell me your output.
Okay my mistake was coping only one file and not all contained in the folder. Now it works but doing Rscript C:\my_directory\my_script.R a windon error from R appears with "installation problem"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.