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?
Related
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)"
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.
I am trying to run a R script on Linux using Rscript command. However, it gives me an error stating that no such file or directory.
Weirdly, when I use command "less" on that R script, it works and displays the script on the screen.
Can anyone explaining this to me ?
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\'"')
I'm working with R for a while, and I always worked with Rstudio, I tried just now to run a Rscript command in terminal (I have a mac..) and I got this error-
>Rscript script.R
-bash: Rscript: command not found
when I tried to open R in the terminal I go the same error-
>R
-bash: R: command not found
I can run R code with the Rstudio and the R application, but I know there is a way to run R throw the terminal.
Did I miss something when I installed R on my computer? do I need to add R to my PATH?
thanks in advance!
Steps to run R script through Windows command prompt
Set the PATH variable for Rscript.exein the environment variables. Rscript.exe can be found inside bin folder of R. Set the path for Rscript.exe to use Rscript command in Windows command prompt. To check if Rscript.exe has been set environmentally or not, type Rscript in command prompt. The follwoing message should come.
Go to Command Prompt, set the path where your .R file is there.
Run the following command: Here abcd.R is present under Documents folder. So I set path and then run Rscript abcd.R
For those who stumbled upon this but use a mac, you might find this useful. I recently downloaded and installed R and RStudio through the CRAN site. I didn't do it through homebrew. Since I downloaded this install directly from the site, it DID NOT add the RScript executable to my /usr/local/bin directory.
I have locate on my mac so I did a quick lookup:
locate RScript
And I found it here:
/Library/Frameworks/R.framework/Versions/4.0/Resources/bin/Rscript
What I had to do was create a symbolic link to my /usr/local/bin directory to get it to work:
cd /usr/local/bin
ln -s /Library/Frameworks/R.framework/Versions/4.0/Resources/bin/Rscript Rscript
Now I'm able to run Rscript through the command line. This may help someone else out there.