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

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.

Related

R script to Task Scheduler

I have an R script that gets data from databases on another server and brings it into my database. I have it saved as "dataimport.R"
I followed a few answers from here and from other websites and created a batch file like this:
"C:\Program Files\R\R-3.4.0\bin\R.exe" CMD BATCH --vanilla --slave "C:\dataimport.R"
This is not working. The cmd window opens up but the tables are not recreated and I dont get any error. I wanted to run the Task Scheduler to automate the process. Any ideas on how to fix this?
I kept at it and interestingly the answer to this was this:
"C:\Program Files\R\R-3.4.0\bin\R.exe" "C:\dataimport.R"
I dont know the reason for this but as long as it works.
I've had loads of problems with this, but finally managed to get it to work. To be more comprehensive, here are some of the things I've tried (in case one of these work for other persons):
#echo off, R CMD BATCH C:\myfolder\script.R
R CMD BATCH C:\myfolder\script.R
Using the package taskschedulerR (somehow didn't work overnight)
Used the answer provided above ("C:\Program Files\R\R-3.4.0\bin\R.exe" "C:\dataimport.R")
and all kinds of variations and combinations off these. (can't remember them all exactly)
What finally worked was:
Make an R script and save it (C:\myfolder\Test.R for example)
Through notepad, fill in: "C:\Program Files\R\R-3.5.2\bin\x64\R.exe" CMD BATCH "C:\myfolder\Test.R" (also tried Rscript.exe, didn't work for me).
in Windows Task Scheduler (v1.0) do 'Create task'
fill in time triggers.
in Actions, make an action with Start a program and in the Program/Script line provide the location where your bat script is. C:\myfolder\Test.bat
in the Start in (optional) line: enter C:\myfolder\
Note: both your .bat file and .R script are in the "C:\myfolder" folder.

When trying to run R script from CMD, getting "Fatal error: cannot open file 'CMD': No such file or directory"

I'm trying to create a batch file to run an R script for me automatically on Windows 7.
Batch file:
"C:\Program Files\R\R-3.2.3\bin\Rscript.exe" CMD BATCH "C:\Users\<my username>\Documents\R\Script Testing\script.R"
pause
R script:
write.csv("hello", "automatic_output.txt")
This script works when I run it in R, but when I try to run the .bat, I get this error:
Fatal error: cannot open file 'CMD': No such file or directory
This happens regardless of whether I paste the code into the command prompt, run the .bat as administrator, or schedule the task.
I tried following the advice given in this thread:
Exporting .csv from R & Batch file
but am seeing the same behavior.
I also used the Windows search to try to find "automatic_output.txt" in case it was being stored in an unexpected location but no results came up.
Lastly,
https://stackoverflow.com/a/23514987 seemed to suggest that I could omit the "CMD BATCH", and when I do I no longer get an error, but automatic_output.txt is still not produced.
Any ideas?
As Thomas pointed out, I needed to change "Rscript.exe" to "R.exe". For future googlers, here is the line of code that worked:
"C:\Program Files\R\R-3.2.3\bin\R.exe" CMD BATCH "C:\Users\<my username>\Documents\R\Script Testing\script.R"

Can I start an Rcmdr session from a unix shell?

I want to launch Rcmdr as a command from bash (or any unix shell), perhaps as an alias. R accepts the CMD argument and I could also pipe a script in with <. I would like the R console to stay open, and an interactive RCommander session to be started (Rcmdr is a popular GUI for R, for any newbies reading along, and it seems that you start up R, type library(Rcmdr) and then Commander() to start it up).
I am aware of how to add Rcmdr to my profile, and it appears to always start up if I include library(Rcmdr) in my .Rprofile, on my Linux workstation.
If I pipe my input in with < then this script works up to the point where it says that Commander GUI is launched only in interactive sessions:
library(Rcmdr);
Commander();
However if I run R CMD BATCH ./rcommander.r it just starts up and shuts down immediately, probably giving me some warning about interactive sessions that I didn't see, because CMD BATCH puts R into non-interactive mode and is thus useless for the purpose of "injecting" Rcmdr into an interactive R session.
It appears impossible to "source a file on the command line but run interactively" in R. It also appears that there are command line options to ignore the global and the user profile, but not to specify a custom profile like R --profile-custom ./.Rprofile2
Either I would like to specify a profile that means "Right now I want to start up and use RCmdr" and still be able to run R without it sometimes.
Working on an Ubuntu machine here, I was able to use the advice provided by Dirk in this mailing list post:
nathan#nathan-laptop:~/tmp$ cat rcommander.r
#!/bin/bash
r -lRcmdr -e'while(TRUE) Commander();'
nathan#nathan-laptop:~/tmp$ cat rcommander2.r
#!/bin/bash
Rscript --default-packages=Rcmdr -e 'while(TRUE) Commander();'
The first script uses Dirk's littler package, available on CRAN, and the second uses the standard Rscript executable. As noted, you can kill the process with ctrl + c from your terminal.

Using R and with the Terminal

I have combed the internet looking for a tutorial on how to use R through the terminal and I can't find anything that exactly fits what I'm looking for. I have a group of R scripts. They need to be run in a specific order. Each produces a set of .csv files to be used as inputs for the next script that will be executed.
I have used R CMD BATCH filename.R and it makes an output file called filename.Rout
But then when I try to use cat filename.Rout, I get an error Fatal error: cannot open file 'filename.R': No such file or directory
Also I'm not sure where my output (.csv files) should be written to. In my R script, there are some write.csv lines pointing the new datasets to be written to a specific directory but when I run R CMD BATCH filename.R in the Terminal, I don't see the files where they should be. I see a filename.Rout in another directory but I can't open it.

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