


OPENR Connect to an R server process.
STATUS = OPENR connects to an R server process. If there is an existing
R server process a warning will be given. STATUS is set to true if
the connection was successful, false otherwise.
[STATUS, MSG] = OPENR returns any error or warning messages in the
output MSG and does not throw warnings. Note that error messages from
the server can by quite cryptic.
[STATUS, MSG, HANDLE] = OPENR returns the handle of the R COM server
connection.
The connection to R is made via the R (D)COM Server. This can be
downloaded from http://cran.au.r-project.org/contrib/extra/dcom or
other CRAN mirror sites. These functions were tested with version 1.2
of the (D)COM Server. Not all R data types are supported by the (D)COM
Server. Version 1.2 supports scalars (booleans, integers, doubles and
strings) and arrays of these.
Example:
status = openR;
% Run one of the R demos to test the connection.
evalR('demo("persp")');
% Now copy the volcano data into MATLAB
volcano = getRdata('volcano');
% Use SURF to plot the volcano
surf(volcano);
axis off; view(-135,40);
% You can also copy the colormap from R
cols = char(evalR('terrain.colors(20)'));
red = hex2dec(cols(:,[2 3]));
green = hex2dec(cols(:,[4 5]));
blue = hex2dec(cols(:,[6 7]));
colormap([red,green,blue]/256);
% Close the connection.
closeR;
See also: CLOSER, EVALR, GETRDATA, PUTRDATA.

0001 function [status ,msg, handle] = openR 0002 %OPENR Connect to an R server process. 0003 % 0004 % STATUS = OPENR connects to an R server process. If there is an existing 0005 % R server process a warning will be given. STATUS is set to true if 0006 % the connection was successful, false otherwise. 0007 % 0008 % [STATUS, MSG] = OPENR returns any error or warning messages in the 0009 % output MSG and does not throw warnings. Note that error messages from 0010 % the server can by quite cryptic. 0011 % 0012 % [STATUS, MSG, HANDLE] = OPENR returns the handle of the R COM server 0013 % connection. 0014 % 0015 % The connection to R is made via the R (D)COM Server. This can be 0016 % downloaded from http://cran.au.r-project.org/contrib/extra/dcom or 0017 % other CRAN mirror sites. These functions were tested with version 1.2 0018 % of the (D)COM Server. Not all R data types are supported by the (D)COM 0019 % Server. Version 1.2 supports scalars (booleans, integers, doubles and 0020 % strings) and arrays of these. 0021 % 0022 % Example: 0023 % 0024 % status = openR; 0025 % % Run one of the R demos to test the connection. 0026 % evalR('demo("persp")'); 0027 % % Now copy the volcano data into MATLAB 0028 % volcano = getRdata('volcano'); 0029 % % Use SURF to plot the volcano 0030 % surf(volcano); 0031 % axis off; view(-135,40); 0032 % % You can also copy the colormap from R 0033 % cols = char(evalR('terrain.colors(20)')); 0034 % red = hex2dec(cols(:,[2 3])); 0035 % green = hex2dec(cols(:,[4 5])); 0036 % blue = hex2dec(cols(:,[6 7])); 0037 % colormap([red,green,blue]/256); 0038 % % Close the connection. 0039 % closeR; 0040 % 0041 % See also: CLOSER, EVALR, GETRDATA, PUTRDATA. 0042 0043 % Robert Henson, May 2004 0044 % Copyright 2004 The MathWorks, Inc. 0045 0046 status = false; 0047 msg = ''; 0048 0049 % Use a global variable to keep track of the connection handle. 0050 global R_lInK_hANdle 0051 0052 % Check if a connection exists 0053 if ~isempty(R_lInK_hANdle) 0054 msg = 'Already connected to an R server.'; 0055 if nargout < 2 0056 warning('Already connected to an R server.'); 0057 end 0058 else 0059 % if not, call the StatConnector and initialize an R session 0060 try 0061 R_lInK_hANdle = actxserver('StatConnectorSrv.StatConnector'); 0062 R_lInK_hANdle.Init('R'); 0063 status = true; 0064 catch 0065 status = false; 0066 R_lInK_hANdle = []; 0067 if nargout == 0 0068 error('Cannot connect to R.\n%s',lasterr); 0069 else 0070 msg = lasterr; 0071 end 0072 end 0073 end 0074 % deal with outputs 0075 if nargout > 2 0076 handle = R_lInK_hANdle; 0077 end 0078 if nargout ==0 0079 clear status 0080 end 0081