I am on a windows machine and want to run a file called main.r so in the command line I put:
"C:\Program Files\R\R-3.0.2\bin\r.exe" CMD BATCH "C:\Users\myname\Desktop\R Projects\soultion\MAIN.R"
when I run this nothing happens. The command line just goes to the next line.
Can you advise what I am doing wrong?
Thank you.
That is what is supposed to happen. Output gets piped to MAIN.Rout in your case. If you want the output to display in the terminal use Rscript instead of R CMD BATCH.
Related
I am new to R script.
I found at the beginning of the R script,
#!/conda_env/myenv/bin/Rscript
library('httr')
May I know what does it mean #!/conda_env/myenv/bin/Rscript?
What happen if I do not add #!/conda_env/myenv/bin/Rscript?
Thank you
The symbol #! is known as a shebang.
When a script starts with #!/conda_env/myenv/bin/Rscript you can run it from your terminal without specifying that it needs Rscript. If your file is called myfile.R you can run it with
/path/to/myfile.R
If you remove this line you have to run the file with the command
/conda_env/myenv/bin/Rscript /path/to/myfile.R
Edit: If /path/to/myfile.R cannot run you must make it executable. In your terminal this can be done with the command chmod u+x myfile.R.
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'm trying to basically run the following .bat file as a scheduled task, while also logging errors in a .txt file:
In the 'program/script' box, I just have cmd. Then in the add arguments box I have:
/k ""T:\Some_folder\mybatchfile.bat" >>"T:\somelog.txt" 2>&1"
This had been working just fine originally before I tried to add the log function and calling cmd explicitly as seen in several posts, but I'd really like to add this function. I'm using /k for now so that I can watch the cmd window as things happen, but plan to replace it with /c so it closes when its done.
I tried many permutations of where my quotation marks are but am not having a lot of luck. I'm also intentionally using >> vs > in order to append the log, not overwrite it.
The contents of the .bat file are basically:
"C:\RDirectory\R.exe" CMD BATCH "T:\Some_folder1\Preworkforbatch.R"
copy T:\Some_folder2\some_data.csv "C:\Users\ABC1\Another_folder"
copy T:\Some_folder3\some_more_data*.csv "C:\Users\ABC1\Another_folder"
I'm wondering if part of it is that T is a network folder that is mapped? Thanks for your help.
edit:
Here is more info on the task:
"T:\Some_folder\mybatchfile.bat" >> "T:\somelog.txt" 2>&1
When redirecting the output of a batch file to a log file you will not see as much output in the cmd window. You have to repeatedly open, close, open the log file to see your progress. Use the title command in your batch to display progress info in the cmd window.
title This is %~F0. The time is %time%. The date is %date%. SLEEP 3600 FOR :RECORDRADIO
Ok this ended up working for me, by editing the batch file itself, and just running the batch file (not cmd explicitly) in the task scheduler:
mybatchfile.bat:
#echo on
"C:\RDirectory\R.exe" CMD BATCH "T:\Some_folder1\Preworkforbatch.R" >> "C:\Users\ABC1\Logfolder\mylog.txt" 2>&1
copy T:\Some_folder2\some_data.csv "C:\Users\ABC1\Another_folder" >> "C:\Users\ABC1\Logfolder\mylog.txt" 2>&1
copy T:\Some_folder3\some_more_data*.csv "C:\Users\ABC1\Another_folder" >> "C:\Users\ABC1\Logfolder\mylog.txt" 2>&1
Writing the log file to the network was causing errors. Writing to the local computer solved this issue. Not using double quotes also was key, instead just quotes around the file/path. This setup gives me an output for each line, so for every line 2>71 shows up, I get an output if there's an error or a completion message.
This is what the task scheduler looks like:
So I've written an R Script that opens up the CMD do notify you when a certain condition is met:
system('CMD /C "ECHO Condition Met"',
invisible=FALSE, wait=FALSE)
I am trying to automate it using a batch file with the following code
start "C:\Program Files\R\R-3.4.0\bin\R.exe" CMD BATCH
C:\Users\User\Documents\filename.R
When I run the R file by itself, the CMD prompt saying that the condition is met pops up just fine. However, when I run the batch file, the CMD prompt saying that the condition is met no longer pops up. Instead, a blank CMD pops up. How do I fix this?
The start command uses the first pair of quotes as the window title
As stated by start /?:
START ["title"] [/D path]"title" Title to display in window title bar.
To overcome this, simply add quotation marks to the start command
Updated Script:
start "" "C:\Program Files\R\R-3.4.0\bin\R.exe" CMD BATCH
C:\Users\User\Documents\filename.R
While running R code in batch mode i need messages/errors/logs from R code to be shown on console screen instead of .Rout file.
I tried all the posts(links copied below) from stack overflow and none of them are showing me desired output.
running-r-in-batch-mode-print-to-screen?
unable-to-run-r-script-through-bat-files-in-windows-server
r-cmd-batch-output-in-terminal
batch code: i tried with tty0 as well.
"C:\Program Files\R\R-3.2.2\bin\R.exe" CMD BATCH --slave "Test.R" /dev/tty
Test.R code
print(1:10)
It will be very helpful if some has can help me in showing the correct way of writing the code to show the output in console screen.And i am working on windows environment.
Any help is much appreciated, thanks.
You can redirect the log that you want to print using stdout or stderr. Sample way to do that will be:
write("Hello World!!", stderr())
Here I am redirecting "Hello World!!" to stderr. If you use stdout() instead of stderr() in code snippet above then ideally it should write log to stdout. However I have seen some issues when trying to write into stdout.
I found out solution to my own question with the help of #shayaa and #abhiieor.
In case if some one needs to see their output in console they are suppose to use Rscript instead of R CMD BATCH in executable .bat file.
batch code:
"C:\Program Files\R\R-3.2.2\bin\Rscript.exe" Test.R
pause
Test.R code
print("-Hello World!!",stdout())
In above code stdout() will redirect output to console.