Reputation: 191
Is it possible to call R scripts in a MATLAB program? How can I do that?
Upvotes: 17
Views: 16760
Reputation: 20643
Another way RWiki recommended:
CurrentDirectory=strrep(pwd,'\','/');
eval(['!C:\R\R-3.0.1\bin/Rscript "' CurrentDirectory '/Commands.R"'])
Upvotes: 1
Reputation: 84
After using R(D)COM and Matlab R-link for a while, I do not recommend it. The COM interface has trouble parsing many commands and it is difficult to debug the code. I recommend using a system command from Matlab as described in the R Wiki.
Upvotes: 6
Reputation: 20560
Yes. On Windows, I have done a lot of this via the Matlab R-link and then R(D)COM server on the R side.
It works beautifully for passing commands and data back and forth. Calling R via the OS is feasible, but then you have to deparse (write) and parse (load) data passed between them. This is tedious and no fun. Especially if you are much data around. It also means that you lose state on the R side and every invocation is just like the first time.
On Linux or another OS, or even for more general usage, I'd now try Rstudio as a server -- see http://www.rstudio.org/docs/server/getting_started for more info.
Upvotes: 2
Reputation: 121077
system
is almost definitely the way to go, as described in other answers. For completeness, you could also use MATLAB's capability to run Java code, and JRI or RCaller to call R from Java. Similarly, you can use MATLAB's capability for running .NET code and R.NET.
Upvotes: 3
Reputation: 12107
You can use R in batch mode. If R is in your path, then you can call from MATLAB:
system('R CMD BATCH infile outfile');
will run the code in infile and place output in the outfile.
EDIT: You can also give it a try with another approach using a R package rscproxy and R(D)COM Server, described here.
Upvotes: 7
Reputation: 126
You could use the system
command to execute R scripts. Something like the following:
[status] = system('R CMD BATCH [options] script.R [outfile]')
where [options] are the options your send to the R interpreter, and [outfile] is your output file.
Upvotes: 1
Reputation: 27017
You can run command line functions in matlab using the unix
command. The easiest way would probably be to set up an R script which outputs results to a text file, run the script in matlab using the unix
command, and then (in matlab) verify that the file exists and load it up.
Upvotes: 0