Running command prompt from R when running R from command prompt - r

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?

Related

gnome-terminal execute multiple R commands by invoking R environment

I would like a shell script that launches a new terminal, opens the R environment in the terminal and executes commands in the R environment of the new terminal.
I found a similar Q in the below link,
In order : launch new terminal, launch R environment in the new terminal, execute commands in R environment
But the solution discussed in the thread (Rscript) doesn't work for me.
I'm trying to run the below code,
gnome-terminal -- /bin/sh -c 'R;Sys.getenv("PATH")'
But I'm getting an error when I use (Sys.getenv("PATH")) after R command. I guess it is due to /bin/sh.
gnome-terminal -- /bin/sh -c 'R'
When I run the above code, it opens another terminal and invokes R environment successfully.
Can someone advise how to execute multiple R commands in gnome-terminal.

Run R Script in Batch File

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

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

In order : launch new terminal, launch R environment in the new terminal, execute commands in R environment

I would like a shell script that launches a new terminal, opens the R environment in the terminal and executes commands in the R environment of the new terminal.
Here's what I did which is not working :
#!/bin/sh
for i in $(seq 25)
do
gnome-terminal -x sh "R; source('source.r'); function($i)"
done
Where, function() is an r function in the file "source.r"
Please help.
N.B. I don't want to launch the program using the command "Rscript"
EDIT 1 : I don't want to use the command Rscript because the execution halts after sometime (don't know why). In an R environment the script works fine though. Here's what I tried with the command Rscript :
#!/bin/sh
for i in $(seq 25)
do
gnome-terminal -e "Rscript script.r $i"
done
EDIT 2 : I found the reason why the script execution halted with the command Rscript. It was a bug in the code. Now I can make things work with what I did in EDIT 1. Would be nice to know if things can be made to work by not using Rscript i.e. launching R in different terminals and executing commands in each terminal from the shell script.

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.

Resources