


EVALR Run an R command.
RESULT = EVALR(COMMAND) evaluates R command COMMAND and saves the
output of the command in RESULT.
[RESULT,STATUS] = EVALR(COMMAND) returns true if the command executed
without error, false otherwise.
[RESULT, STATUS, MSG] = EVALR(COMMAND) returns any error messages.
[RESULT, STATUS, MSG] = EVALR(COMMAND, 0) is used to get the
status when executing R commands such as sourcing files or running
demos that do not return any result.
Example:
status = openR;
% Generate some random numbers.
x = evalR('runif(5)')
% Create a MATLAB variable and export it to R.
a = 1:10;
putRdata('a',a);
% Run a simple R command using the data
b = evalR('a^2')
% Run a series of commands and import the result into MATLAB.
evalR('b <- a^2');
evalR('c <- b + 1');
c = getRdata('c')
% Close the connection.
closeR;
See also: CLOSER, GETRDATA, OPENR, PUTRDATA.

0001 function [result,status,msg] = evalR(command,noreturn) 0002 %EVALR Run an R command. 0003 % 0004 % RESULT = EVALR(COMMAND) evaluates R command COMMAND and saves the 0005 % output of the command in RESULT. 0006 % 0007 % [RESULT,STATUS] = EVALR(COMMAND) returns true if the command executed 0008 % without error, false otherwise. 0009 % 0010 % [RESULT, STATUS, MSG] = EVALR(COMMAND) returns any error messages. 0011 % 0012 % [RESULT, STATUS, MSG] = EVALR(COMMAND, 0) is used to get the 0013 % status when executing R commands such as sourcing files or running 0014 % demos that do not return any result. 0015 % 0016 % Example: 0017 % 0018 % status = openR; 0019 % % Generate some random numbers. 0020 % x = evalR('runif(5)') 0021 % % Create a MATLAB variable and export it to R. 0022 % a = 1:10; 0023 % putRdata('a',a); 0024 % % Run a simple R command using the data 0025 % b = evalR('a^2') 0026 % % Run a series of commands and import the result into MATLAB. 0027 % evalR('b <- a^2'); 0028 % evalR('c <- b + 1'); 0029 % c = getRdata('c') 0030 % % Close the connection. 0031 % closeR; 0032 % 0033 % See also: CLOSER, GETRDATA, OPENR, PUTRDATA. 0034 0035 % Robert Henson, May 2004 0036 % Copyright 2004 The MathWorks, Inc. 0037 0038 global R_lInK_hANdle 0039 result = []; 0040 msg = ''; 0041 0042 % For some reason there are two methods for evaluating commands -- Evaluate 0043 % and EvaluateNoReturn. These seem to do the right thing until the output 0044 % handling is reached at which point EvaluateNoReturn errors if outputs 0045 % were requested and Evaluate errors if no outputs were returned. 0046 0047 try 0048 if nargout == 0 || (nargin == 2 && noreturn == 0) 0049 R_lInK_hANdle.EvaluateNoReturn(command); 0050 else 0051 result = R_lInK_hANdle.Evaluate(command); 0052 end 0053 status = true; 0054 catch 0055 status = false; 0056 msg = lasterr; 0057 if nargout == 0 0058 error('Problem evaluating command %s.\n%s',command,msg); 0059 end 0060 end 0061 0062 if nargout == 0 0063 clear result; 0064 end