Running R script through .bat file on windows 8.1? - r

I'm trying to run a R script through a .bat file, and I only managed to open the file and not to run the script.
These lines are what I use in the .bat file.
"C:\Program Files\R\R-3.3.3\bin\Rscript.exe"
"C:\Users\Documents\Script\Script1d.R" --vanilla --slave
What am I missing here?

Related

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)"

Scheduling an R Script

My team and I are using a .bat file
#echo off
"C:\Program Files\RStudio\bin\rstudio.exe" R CMD BATCH --vanilla --slave "C:\Users(user name)\Hello.R"
to execute a script that reads...
print("Hello World!")
When we run the .bat file, Rstudio opens, it presents the script, but it does not run the script automatically to present the final result in the counsel.
What am I doing wrong?
Thanks

Error in .BAT file R Script

I have a .bat file as follows. task.bat
#echo off
R CMD BATCH C:\Users\Raghavan\Desktop\MyTask.R
The file needs to run an R script. The following is the R script.
A<-read.csv("C:\Users\Raghavan\Desktop\A.csv")
write.csv(A, file = "B.csv")
The R script runs by itself. However, when I try running it through the .bat file or through windows scheduler, the script fails to run. I have tried many sites.I request someoen to see if this error can be fixed. Most likely, the error is in the .bat file
I run my R files with the following command inside of the .bat script:
#echo Off
"C:\Program Files\R\R-3.4.2\bin\R.exe" CMD BATCH --vanilla --slave "C:\File.R"
Define the full paths to executable R and to the file that you wanna run. Hope this helps.
See also ?BATCH and this resource on arguments of R CMD BATCH.
I made the following edits to the code.
# echo off
"C:\Program Files\R\R-3.3.2\bin\R.exe" R CMD BATCH "D:\Newfolder\task1.R"
The files were then shifted to the D drive. This resulted in the code working.

Running a .bat file from R - had status 2

I am trying to run a .bat file, which does run perfectly when I double click in it (Windows OS), but fails when I try to run it in R
com <- "C:\\SASLocal\\RUN3614\\56a8c11b-84b2-4af7-a155-01190936b1c1\\M1_superGOtest.bat"
system(com)
I am getting back a message of had status 2
Just an FYI, this triggers a SAS program, which I need to run in SAS as it is for comparison purposes between SAS and R.
In Windows, to run batch files from command line you need to call a command line interpreter, Command Prompt or PowerShell, passing batch file as an argument.
A .bat script by itself is like an .R script and does not do anything until an executable runs it (i.e., Rscript.exe, R.exe, Rcmd.exe, Rterm.exe) and in this case, cmd.exe and powershell.exe:
# COMMAND PROMPT
system('cmd /c "C:\\SASLocal\\RUN3614\\56a8c11b-84b2-4af7-a155-01190936b1c1\\M1_superGOtest.bat"')
# POWERSHELL
system('powershell -c & "\'C:\\SASLocal\\RUN3614\\56a8c11b-84b2-4af7-a155-01190936b1c1\\M1_superGOtest.bat\'"')

Rscript and UNC paths

I have an R script that I would like to run from the command line using Rscript.exe in Windows. The script runs fine if I open it in RStudio but when I run it through Rscript in the command line it fails because I have UNC paths in my script. For example,
read.csv("//server/directory/file.csv/")
Rscript fails and says the directory does not exist. Is there anyway to resolve this problem?

Resources