Run R Script in Batch File - r

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

Related

How to show progress step in cmd console for executing .r file

When I run my .r file using batch cmd, I expect to read progress notes(not bar) just like in Rstudio console. it seems what is shown in cmd is just the message in red colour in RStudio, whether just warning/or info or when it's an error.
How to show the notes in blue is also shown so I can read what exactly is running. later i will output it into log file.
It's good if nothing wrong happens then nothing is shown, but a little notes will be useful too
notes in RStudio
in cmd console
You may want to try calling R CMD BATCH instead of Rscript, e.g.,
"<R_location>\bin\R.exe" CMD BATCH "your_script.R"
which will generate a your_script.Rout file in the working directory, which will contain everything you see in RStudio.

How to debug this scheduled .bat task in task scheduler using cmd and log file?

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:

Running command prompt from R when running R from command prompt

I am running an R script on the command prompt like this:
> "R.exe" CMD BATCH --vanilla --slave "myscript.R"
The code runs fine in RStudio, but when I run it from command prompt, it stops running after a certain line of code. What's interesting is that the last line that works is a line that itself calls command prompt:
system("cmd /K my_exe_file")
That line actually runs successfully, but then the next line doesn't run. There's no error message. Instead, it keeps appearing on command prompt as if the R code is running indefinitely, but in reality nothing is happening.
Is there a problem with calling command prompt when running R from command prompt? Or is there a different issue I'm missing?

How to kick off R file with command line

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.

gWidgets GUI cannot display when called with R CMD BATCH

I developed an analyzer with GUI (utilizing gWidgets package). Everything seems good when I run my code in R console or R studio, GUI can popup as expected, interaction goes smoothly by choosing options.
However, my manager has no idea about coding stuff, and what he wants is click-N-run. So I tried to use R CMD BATCH to create .bat file.
R CMD BATCH G:\Temp\dav\AB_Analyzer\MAINcode.r outputFile
When I ran the bat file, there is nothing popping up.
May I know what I did wrong?
Thanks for any help.
If you run an R script in batch mode (R CMD BATCH) the "interactive flag" is set to false which may trigger this behaviour (no user interaction = do not show any GUI).
You can query the "interactive flag" with the interactive() function in R.
Possible solution: Add the --interactive parameter to the command line.
To test his behaviour create an R script file with the following content:
print(interactive())
If you run this script with
R CMD BATCH --no-save --no-restore batch_test.R out.txt
You will find the result FALSE in the out.txt file, if you run it with
R --vanilla --interactive < batch_test.R
You will see a TRUE (so use the last command line as solution - note: without CMD).

Resources