Execute batch file from Matlab - r

I have a Matlab function that finds the path where this function is within my pc and then runs a bat file on that same directory. This bat file is meant to execute an R script but for a strange reason is failing to do so.
This is my Matlab function:
function [] = myFunction(arg)
% Find the directory of the executing script
thisDir = fileparts(mfilename('fullpath'));
% Save arg as a csv on this directory, this will be read by my R script
tmpDir = strcat(thisDir,'/tmp.csv');
csvwrite(tmpDir,arg);
% Specify the command to run
dosCommand = ['call "' thisDir '/runRscript.bat"'];
dos(dosCommand);
end
The bat file has the following code:
"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH runRScipt.R
When I run the function in Matlab I get the below message:
C:\Users\...mypath...>"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH
runRscript.R
Since I get this message in Matlab I have no doubt it is finding and reading the batch file, but it fails to execute the R script. I know the bat file works as expected since I can run it through the command line (with the command that should be the "dosCommand" on the Matlab script) or by clicking twice on the .bat file.

I found the answer. For a strange reason the dos() command would not work, but the system() command will do the job. Then the code will look like this:
function [] = myFunction(arg)
% Find the directory of the executing script
thisDir = fileparts(mfilename('fullpath'));
% Save arg as a csv on this directory, this will be read by my R script
tmpDir = strcat(thisDir,'/tmp.csv');
csvwrite(tmpDir,arg);
% Specify the command to run
sysCommand = ['call "' thisDir '/runRscript.bat"'];
system(sysCommand);
end
And the batch file:
"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH runRScipt.R

Instead of R.exe please try Rscript.exe. R.exe runs R code in interactive mdoe while Rscript runs the code in batch mode. Ideally you should find Rscript executable in the same path as in R executable (i.e. "C:\Program Files\R\R-3.2.2\bin\x64" in your case)

Related

How to "source" (not run line by line) an R script from the Windows command line

I am creating an R script myscript.R which manipulates an excel file by means of XLConnectpackage.
The point is it refers to several external files: the excel file itself and another R file (functions library), so I need to set a working directory in the location of the script (so that relative paths to external files work properly).
I am using the following in my script
new_wd <- dirname(sys.frame(1)$ofile)
setwd(new_wd)
When I source the script from my RStudio it gets the job done. The problem is that the script is to be used by non-programmers, non-Rstutio-users, so I create .bat file (which I want to turn into an .exe one)
"C:\Program Files\R\R-4.0.3\bin\Rscript.exe" "C:\my\path\to\myscript.R"
It executes the script line by line but sys.frame(1) only works when sourcing.
How could I solve it?
Thanx
I have found a solution and it works properly.
From CMD command line or from a .bat file one can add an argument -e to the command, so that you can use a R language.
absolute\path\to\Rscript.exe -e "source('"relative\path\to\myscript.R"')"
It worked for me.
Besides, as Compo commented, I think there's no need for a .exe file, since a .bat does the job.

Why .bat file doesn't initiate R file

I would like to invoke R script by using a .bat file.
For testing, I saved an R file on the desktop in the name print_test.R
The code in this file is:
p<-500
write.csv(p,"print.csv")
I wrote the .bat file :
#echo off
R CMD BATCH C:\Users\User\Desktop\print_test.R
UPDATE:
After adding pause to the .bat file I got the following text:
"'R' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . ."
When I click on the bat file (also saved on the desktop) there is a blink of the CMD window but the print.csv file is not created.What is wrong?
Consider using the automated utility, Rscript.exe, as the R CMD BATCH might be considered a legacy command. See post: R.exe, Rcmd.exe, Rscript.exe and Rterm.exe: what's the difference?
Rscript as an environment PATH variable:
#echo off
Rscript "C:\Users\User\Desktop\print_test.R"
Rscript not as a environment PATH variable (where you specify full path of the executable -usually in bin folder of installation):
#echo off
"C:\Path\To\Rscript" "C:\Users\User\Desktop\print_test.R"
OR if paths do not have spaces:
#echo off
C:\Path\To\Rscript C:\Users\User\Desktop\print_test.R
I followed Sumedh's tip :
here is the new code:
#echo off
"C:\Program Files\R\R-3.2.4\bin\x64\r.exe" CMD BATCH "C:\Users\User\Desktop\print_test.R"

create R script files, Saving as R script file and opening R script file from command line

I am using R from a distant linux server. I want to make an R script file, open an scripts files and edit them. However, I can just use the R command line. I have no idea how I can create an R script file like in RGui, or how to open such files for editing.
There are multiple non graphical editors to edit your file that you have access to in command line, like nano or vim.
To create an Rscript file, simply add #!/usr/bin/env Rscript at the top of your .R file and make it executable.
Also see Run R script from command line

How run shell script from R environment

I have raw micro-array expression data files with extension .CEL, but for few files the extension is somehow \ .CEL (ie. name space dot CEL). I have made a simple shell script that replaces file names correctly on ubuntu terminal, but I do not have any idea of using it in R environment. I have even tried it using system() command to execute shell script, but did not work for me.
Shell Script I have written is as follows:
for file in *.CEL;
do mv "$file" "${file//[[:space:]]}";
done

How do I run a .bat file without a terminal (remaining open)?

I want to run a jar file with a .bat (the jar file doesn't seem to want to open on its own but thats a different issue for now) but as the java file runs for a long time, the command prompt remains open (while the .bat/.jar is still running)
I do not want this.
I read somewhere that you can use a .cmd file and the command(s):
cmd /c bat.bat
exit
To run a bat file without a command prompt. But that isn't working for me. When I click the .cmd program it just opens a command promopt and keeps printing "cmd /c bat.bat exit" over and over in a loop.
What am I doing wrong, was my .cmd command wrong? Is there another way to run a .bat without a command prompt remaining open?
Thanks alot.
From here:
Save the following as wscript, for instance, hidecmd.vbs after
replacing "testing.bat" with your batch file's name.
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c testing.bat"
oShell.Run strArgs, 0, false
The reference is here
http://msdn.microsoft.com/en-us/library/d5fk67ky.aspx

Resources